How do you safely deal with range.front?

aliak something at something.com
Fri Dec 29 19:38:44 UTC 2017


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.

In some other languages the concept of "front" or "first" returns 
a safe referece, or optional value that calling methods on will 
not crash the program.

I'm thinking of maybe making something like

safeFront(R)(R r) {
   return r.empty ? SafeRef!(ElementType!R) : SafeRef(r.front);
}

Where SafeRef, I think, can provide an overload for opDispatch 
and just forward the calls to the stored reference of r.front. If 
the stored reference is null then it can return a SafeRef to the 
return value of whatever was being dispatched to.

Is there another way to go about this? Maybe through naturally 
using r.front instead of making a r.safeFront alternative?

Cheers


More information about the Digitalmars-d-learn mailing list