logoalt Hacker News

wahern • today at 1:17 AM • 0 replies • view on HN

You don't necessarily need a result/option type. Go could add a dependent typing facility that let you express a relationship between the first and subsequent return values.

Very few languages do that; an option type is simpler to implement in many respects. But in the case of Go, especially at this juncture, it's possible the simplest route is a dependent type, at least a specialized one. You don't need to introduce a fully generic dependent typing facilty.

Even simpler would just be type qualifers, e.g. optional or nilable, on the first return value, though that doesn't help with multivalue returns, where a simple dependent typing facility could fully express success vs error return semantics.

You could also do it with contracts, though more obliquely. One of the criticisms with C++ contracts was that a contract made it easier to optimize a routine for the declared relationships, but without absolutely mandating enforcement that the callee or caller also obeyed the contracts, which is more difficult to do. That meant there was the potential for contracts to result in more bugs caused by interactions between undefined behavior and the optimizer.