Thread overview
usage of ref foreach with variadic functions fails with "cannot be ref"
Feb 11, 2017
error
Feb 11, 2017
rikki cattermole
Feb 11, 2017
error
Feb 12, 2017
Michael Coulombe
Feb 12, 2017
error
February 11, 2017
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...
  }
}
February 12, 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] = ...;
}
February 11, 2017
On Saturday, 11 February 2017 at 14:43:18 UTC, rikki cattermole wrote:
> Try:
>
> foreach(i, v; vars) {
> 	vars[i] = ...;
> }

Perfect! Thanks so much - I wish that hint was in the documentation for variadic functions, although I guess it suggests an inefficiency in the compiler - since there would be an additional copy of vars[i] created in v.
February 12, 2017
On Saturday, 11 February 2017 at 15:02:11 UTC, error wrote:
> On Saturday, 11 February 2017 at 14:43:18 UTC, rikki cattermole wrote:
>> Try:
>>
>> foreach(i, v; vars) {
>> 	vars[i] = ...;
>> }
>
> Perfect! Thanks so much - I wish that hint was in the documentation for variadic functions, although I guess it suggests an inefficiency in the compiler - since there would be an additional copy of vars[i] created in v.

Do you have a complete code example that gives your error? I can't reproduce it (DMD v2.073.0):

int foo(T...)(T vars) {
    int i = 0;
    foreach(ref v ; vars) {
        v = 5;
        i += v;
    }
    return i;
}
void bar(T...)(ref T vars) {
    foreach(ref v ; vars) {
        v = 3;
    }
}
void main() {
    import std.stdio;
    int x = 7;
    bar(x);
    writeln(foo(4,x,8.2)); // 15, no errors
}
February 12, 2017
On Sunday, 12 February 2017 at 03:34:19 UTC, Michael Coulombe wrote:

> Do you have a complete code example that gives your error? I can't reproduce it (DMD v2.073.0):
>
> int foo(T...)(T vars) {
>     int i = 0;
>     foreach(ref v ; vars) {
>         v = 5;
>         i += v;
>     }
>     return i;
> }
> void bar(T...)(ref T vars) {
>     foreach(ref v ; vars) {
>         v = 3;
>     }
> }
> void main() {
>     import std.stdio;
>     int x = 7;
>     bar(x);
>     writeln(foo(4,x,8.2)); // 15, no errors
> }

Oh... That's really strange;  I've updated my code to use non indexed foreach, and it's now building without the error...  I have absolutely no idea what caused the error to occur.  I've also tried reverting to earlier versions of the project, and I can't recreate the issue.  If it occurs again, I'll snapshot the project and try to make a repeatable set up.