News for hackers. Stuff that matters.
Re:Nanomsg postmortem and other stories by IshKebab on Monday February 08, 2016 @09:21AM (#11058197)
Yeah I dunno, the "solid technical reasons" are pretty unconvincing. The reasons are:

1. Exceptions are awkward because they separate the error handling from the context that is usually required to correctly handle an error (i.e. where it occured).

2. Exceptions are the only reasonable way for a constructor to fail. You can't return error codes from a constructor.

I actually agree with the point about exceptions, and I'm glad Rust didn't use them. That said, the solution is trivial (and the author admits it) - don't use exceptions.

The second point is also actually a fairly good one, and again Rust has fixed this - it uses static `new()` initialisers that create object (or not, if they fail). Fortunately we can actually copy this method in C++. Here's an example for a network connection object:

    class Connection
	{
	public:
		static boost::optional<Connection> connect(string host)
		{
			// Try to connect.
			int connectionHandle = -1;
			int rc = connect(host, &connectionHandle);

			// If it fails return none.
			if (rc < 0)
				return boost::none;
				
			// Otherwise return the new object.
			return new Connection(connectionHandle);	
		}
		
		void send();
		void recv();
		void etc();

	private:
		explicit Connection(int connectionHandle)
		{
			mHandle = connectionHandle;
		}
		
		int mHandle;
	}
Unfortunately as far as I can see there is no Rust-like `result<>` type in Boost or the standard library yet, but it would be fairly easy to write your own.

It seems pretty crazy to give up C++ for these fixable flaws. Especially given how many unfixable flaws C has.

Re:Nanomsg postmortem and other stories by hendzen on Monday February 08, 2016 @09:40AM (#11058292)
Your connect method can still throw std::bad_alloc.
Re:Nanomsg postmortem and other stories by IshKebab on Friday February 12, 2016 @01:03PM (#11089065)
You could easily catch that within the method though. But in general I think throwing an exception on memory allocation failure is much better than the alternative, which is - in practice - assuming it succeeds and segfaulting.
Re:Nanomsg postmortem and other stories by kinofcain on Monday February 08, 2016 @10:20AM (#11058542)
Boost has variant, which is also a discriminated Union though the visitor pattern ergonomics aren't quite as nice as result in rust or doing the same thing with enums in swift. There's work to get something like variant into the standard.