On Friday, 29 January 2016 at 17:44:34 UTC, Chris Wright wrote:
I want to create an opApply for a type.

I've marked my code @safe, because everything I wrote was @safe. The body of opApply is @safe, but it calls a delegate that may or may not be @safe.

How do I make it so I can iterate through this type safely and systemly?

I want to support iteration like:

foreach (string key, string value; collection) {}
foreach (size_t i, string key, string value; collection) {}

You can implement an input range and annotate all the primitives as @safe. Then if there's only an input range in your agregate, DMD will auto-detect that it must use it in foreach():

http://dlang.org/spec/statement.html#foreach-with-ranges

in the worst case (range not implementable directly but only as a getter in .range() or .opSlice() you'll have to change the style a bit and consume the range explicitly in a typical "while (!stuff.empty) {...}"


Reply via email to