September 01, 2019 Re: Input/Output multiple values from function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jabari Zakiya | On Sunday, 1 September 2019 at 20:42:28 UTC, Jabari Zakiya wrote:
> It still won't compile, with this error.
>
> Error: AliasSeq!(modpg, res_0, restwins, resinvrs) is not an lvalue and cannot be modified
>
> Here's a gist of the code.
>
> Top functions in code with issues are genPgParameters and selectPG
>
> https://gist.github.com/jzakiya/9227e4810e1bd5b4b31e949d1cbd5c5d
You can't do multiple assignments at once using AliasSeq; you have to assign each variable individually:
auto parameters = genPgParameters(pg);
modpg = parameters[0];
res_0 = parameters[1];
restwins = parameters[2];
resinvrs = parameters[3];
|
September 02, 2019 Re: Input/Output multiple values from function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | On Sunday, 1 September 2019 at 20:50:42 UTC, Paul Backus wrote:
> On Sunday, 1 September 2019 at 20:42:28 UTC, Jabari Zakiya wrote:
>> It still won't compile, with this error.
>>
>> Error: AliasSeq!(modpg, res_0, restwins, resinvrs) is not an lvalue and cannot be modified
>>
>> Here's a gist of the code.
>>
>> Top functions in code with issues are genPgParameters and selectPG
>>
>> https://gist.github.com/jzakiya/9227e4810e1bd5b4b31e949d1cbd5c5d
>
> You can't do multiple assignments at once using AliasSeq; you have to assign each variable individually:
>
> auto parameters = genPgParameters(pg);
> modpg = parameters[0];
> res_0 = parameters[1];
> restwins = parameters[2];
> resinvrs = parameters[3];
There's still are problem When I do this.
auto parameters = genPgParameters(pg);
modpg = parameters[0];
res_0 = parameters[1];
restwins = parameters[2];
resinvrs = parameters[3];
The compiler says this:
Error: cannot implicitly convert expression parameters.__expand_field_3 of type uint[] to shared(uint[])
If I comment out the last line the programs compiles (and crashes when run since all the parameters aren't available).
So why does it accept parameter[2] but not [3] as they are both defined the same?
Is this a compiler bug?
I have to say, getting this simple parameter passing to work in D has been an infuriating experience. :-(
|
September 02, 2019 Re: Input/Output multiple values from function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jabari Zakiya | On 08/27/2019 10:17 PM, Jabari Zakiya wrote: > I can't do (a, b, c,d) = func1(i) directly. > What do I do to assign the output of func1 to the individual variables? I had some time to play with the following syntax, similar usage of which has been proposed a number of times as a replacement for tuple expansion. Assuming that foo() returns a struct of int, double, string; the following expression will set the variables to those members: int i; double d; string s; foo(42).into!(i, d, s); Here is the complete program: import std.stdio; import std.string; struct S { int i; double d; string s; } S foo(int i) { return S(i, 1.5, "hi"); } template into(args...) { auto into(From)(From from) if (is (From == struct)) { static foreach (i, m; __traits(allMembers, From)) {{ alias argT = typeof(args[i]); alias memT = typeof(__traits(getMember, from, m)); static assert (is (argT == memT), format!"Cannot expand '%s %s.%s' into '%s %s' argument." (memT.stringof, From.stringof, m, argT.stringof, args[i].stringof)); mixin (format!"args[%s] = from.%s;"(i, m)); }} } } void main() { int i; double d; string s; foo(42).into!(i, d, s); writeln(i); writeln(d); writeln(s); } I know that it does not address attributes like shared, etc. but it shows how expressive nested templates can be. Ali |
Copyright © 1999-2021 by the D Language Foundation