usage of ref foreach with variadic functions fails with "cannot be ref"

rikki cattermole via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Feb 11 06:43:18 PST 2017


On 12/02/2017 3:41 AM, error wrote:
> I'm making a serializer that has a variadic write method that takes
> arbitrary params
> and serializes them;  I want to do the same thing with a read method (
> pass in your
> params by ref, and it populates them with data ) -  however, I run into
> a compiler
> error - "cannot be ref" in the foreach statement...  Anyone know a
> workaround for this?  Is this a bug, or by design?
>
> Simplified example:
>
> void populateVars(T...)(T vars){
>   // Generates "cannot be ref" compiler error:
>   foreach(ref v; vars){
>     // Populate v with some data here...
>   }
> }

Try:

foreach(i, v; vars) {
	vars[i] = ...;
}


More information about the Digitalmars-d-learn mailing list