Optional and orElse: design feedback/critique?

Johannes Loher johannes.loher at fg4f.de
Sat Jul 27 18:14:39 UTC 2019


Am 27.07.19 um 19:59 schrieb Johannes Loher:
> It is slightly annoying that you have to write the return type
> explictly, but it's not that bad. I think I also wrote my own orElseThrow:
> 
> ```
> import optional;
> 
> auto orElseThrow(alias fun, T)(auto ref Optional!T opt)
> {
>     return opt.orElse!(delegate T() { throw fun(); });
> }
> 
> void main()
> {
>     no!int.orElseThrow!(() => new Exception(""));
> }
> ```
> (you could make some improvements by checking (with static ifs) if a
> delegate is actually needed, or a function suffices)

By the way, in this situation it would be kind of useful to have a
bottom type or an (inferred) @noreturn annotation. Then in addition
callables that return something of type T, you could explictily allow
passing in callables that never return.


More information about the Digitalmars-d mailing list