www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to use std.range.interfaces in pure safe code

reply Freddy <Hexagonalstar64 gmail.com> writes:
How do I use http://dlang.org/phobos/std_range_interfaces.html in 
pure  safe code?
Oct 02 2015
parent Jonathan M Davis via Digitalmars-d-learn writes:
On Friday, October 02, 2015 19:45:05 Freddy via Digitalmars-d-learn wrote:
 How do I use http://dlang.org/phobos/std_range_interfaces.html in
 pure  safe code?
You don't. None of the functions in those interfaces are marked with safe or pure. One of the problems with classes is that you're stuck with whatever attributes a base class put on a function. If it has an attribute like safe or pure, then the derived class can't do anything that requires that they be system or pure, whereas if it doesn't have those attributes, then it can't be used in code that's safe or pure. For safe, trusted can be used, but that only makes sense if the the code is legitimately safe in spite of the system stuff that it's doing (i.e. the programmer verified it), whereas if it depends on the caller or just plain isn't safe, then you're stuck, and with pure, you're pretty much just stuck. nothrow has the same problems. In general, I would strongly advise against using any of the interfaces in std.range.interfaces and just use structs and templatize your code. Then you don't run into any of those problems, since pure, safe, and nothrow get inferred as appropriate, and it's likely to be more efficient, since you don't end up with any virtual calls, and there are better optimization opportunities (such as inlining). But if you do really need to use interfaces like that, then you can just write your own and put whatever attributes you want on their functions. That won't work if you have to interact with someone else's code that's using those interfaces, but aside from that, it should work just fine. But if you _have_ to use those interfaces, then you simply can't use them in code that has to be safe, pure, or nothrow. - Jonathan M Davis
Oct 03 2015