'unwrap envy' and exceptions

jfondren julian.fondren at gmail.com
Fri Sep 10 16:10:27 UTC 2021


On Friday, 10 September 2021 at 15:57:03 UTC, Paul Backus wrote:
> On Friday, 10 September 2021 at 15:44:04 UTC, jfondren wrote:
>> On Friday, 10 September 2021 at 15:26:18 UTC, Kagamin wrote:
>>> Nullable doesn't replace exceptions, it specifies a value 
>>> which is legitimately absent, not due to an error
>>
>> Asking for the maximum of an empty array is an error case 
>> (hence D's minElement/maxElement thrown an AssertError, not 
>> even a catchable exception)
>
> Strangely, there is no mention of this in the documentation for 
> either `minElement` or `maxElement`. And it's also inconsistent 
> with other similar functions in `std.algorithm.searching`; for 
> example, `minCount` and `maxCount` throw an `Exception` on an 
> empty range.

In a -release build, over an `int[]`, you get a RangeError from 
std.range.primitives.front:

```d
     assert(a.length, "Attempting to fetch the front of an empty 
array of " ~ T.stringof);
     return a[0];
```

So the skipped assert() there and the skipped contract 
programming assert() in minElement/maxElement, all they're really 
doing is improving the error message. That might be why they're 
not mentioned.

... or in a -release -boundscheck=off build, you get Error: 
program killed by signal 11. These kind of flags also weaken the 
assumeWontThrow=unwrap similarity.


More information about the Digitalmars-d mailing list