How do you safely deal with range.front?

aliak something at something.com
Sat Dec 30 19:00:29 UTC 2017


On Friday, 29 December 2017 at 20:11:03 UTC, Seb wrote:
> On Friday, 29 December 2017 at 19:38:44 UTC, aliak wrote:
>> Hi,
>>
>> So when I'm dealing with ranges, there're a number of times 
>> where I get the front of the returned result of a set of 
>> operations, but of course there is no front so you get an 
>> runtime access error.
>>
>> [...]
>
> Do you know about the proposed `orElse`?
>
> https://github.com/dlang/phobos/pull/5154

Oh cool. No I did not. Seems like a nice approach. It would incur 
the creation of a new object when one doesn't exist, which I 
guess would be fine for many situations, and would work for this 
as well probably, but it'd be nice to avoid it as well in some 
situations.

Just going thought std a bit now and I found this: 
https://dlang.org/library/std/typecons/black_hole.html

That would be pretty cool to have 
range.op!f.blackHoleFront.call() - though blackHoleFront sounds 
horrible :p


More information about the Digitalmars-d-learn mailing list