How do you safely deal with range.front?

Mengu mengukagan at gmail.com
Sat Dec 30 19:57:45 UTC 2017


On Saturday, 30 December 2017 at 19:00:07 UTC, aliak wrote:
> On Friday, 29 December 2017 at 20:47:44 UTC, Dukc wrote:
>> [...]
>
> Hmm, interesting. Not sure that's what I'm looking for but I 
> like it anyway :)
>
> I'm more looking to deal with situations like this:
>
> Instead of this:
>   auto result = range.op!f;
>   if (!result.empty) {
>     result.front.method();
>   }
>
> This:
>   range.op!f.ifFront.method();
>
>
> In the above scenario I only want method to be called if the 
> pipeline resulted in any element left in the range.
>
>> [...]
>
> True you don't want to call front on empty, but sometimes I 
> only care to call a method on front if it's there. Where the 
> situation necessitates the existence of front, and not having a 
> front is indeed a bug, then you want the program to terminate, 
> yes. But not otherwise.

it would be actually great in such scenarios if d had a language 
construct for existantial checks. you'd just be able to do 
range.front?.method(). but no, it is not 2017. d does not need it.


More information about the Digitalmars-d-learn mailing list