Safe Usage of Mutable Ranges in foreach scopes

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 8 04:25:25 PDT 2015


I use a lot of file parsing looking like

alias T = double;
T[] values;

foreach (line; File(path).byLine)
{
     foreach (part; line.splitter(separator))
     {
         values ~= part.to!T;
     }
}

The key D thing here is that this is _both_ fast (because no 
copying of file-memory-slices needs to be performed) _and_ 
"high-level". However I'm currently lacking one feature here in 
D. Namely:

A way to qualify `line` to prevent it from escaping the scope of 
the current foreach iteration. And `part` should 
D-style-transitively inherit this property in the same scope as 
`line`.

This because line is a reference to a volatile buffer that 
changes its contents with every foreach-iteration, which is a 
performance design choice which achieves "maximum D performance" 
because it avoids memory reallocation in in each iteration of 
`File.byLine`.

Such a feature would make the usage of this pattern very (perhaps 
even absolutely) safe from a memory corruption point of view.

Could the scope keyword be used here?

Could the work done in DIP-25 be reused here, Walter?

Destroy!


More information about the Digitalmars-d-learn mailing list