Optional and orElse: design feedback/critique?
Alexandru Ermicioi
alexandru.ermicioi at gmail.com
Sat Jul 27 18:54:38 UTC 2019
On Saturday, 27 July 2019 at 17:59:19 UTC, Johannes Loher wrote:
> Am 27.07.19 um 19:15 schrieb aliak:
>
> ```
> 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(""));
> }
> ```
Lazy arguments could fit perfectly for orElseThrow statement:
```D
import optional;
auto orElseThrow(T)(auto ref Optional!T opt, lazy Exception
throwable)
{
if (opt.empty) {
throw throwable;
}
return opt.front;
}
void main()
{
no!int.orElseThrow(new Exception("")); // maybe instead of
no, just optional!int? using just negation is kinda ambiguous.
}
```
More information about the Digitalmars-d
mailing list