Thread overview | ||||||
---|---|---|---|---|---|---|
|
February 09, 2016 How do you reference variables in an AA of Variants? | ||||
---|---|---|---|---|
| ||||
This: double b = 1.0; Variant[string] aa = ["b": &b]; writeln(aa["b"]); fails with: Error: cannot implicitly convert expression(["b":&b]) of type double*[string] to VariantN!20u[string] Helps please! |
February 09, 2016 Re: How do you reference variables in an AA of Variants? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Enjoys Math | On Tuesday, 9 February 2016 at 03:49:11 UTC, Enjoys Math wrote:
> This:
> double b = 1.0;
>
> Variant[string] aa = ["b": &b];
>
> writeln(aa["b"]);
>
> fails with:
>
> Error: cannot implicitly convert expression(["b":&b]) of type double*[string] to VariantN!20u[string]
>
> Helps please!
Use an intermediate to carry the result of &<stuff>:
double b = 1.0;
Variant vb = &b;
Variant[string] aa = ["b": vb];
writeln(aa["b"]);
&b is a rvalue.
vb is a lvalue.
|
February 08, 2016 Re: How do you reference variables in an AA of Variants? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Enjoys Math | On 02/08/2016 07:49 PM, Enjoys Math wrote: > This: > double b = 1.0; > > Variant[string] aa = ["b": &b]; > > writeln(aa["b"]); > > fails with: > > Error: cannot implicitly convert expression(["b":&b]) of type > double*[string] to VariantN!20u[string] > > Helps please! When initializing the array, you have to use Variant(&b): import std.stdio; import std.variant; void main() { double b = 1.5; Variant[string] aa = ["b": Variant(&b)]; writeln(aa); writeln(aa["b"]); writeln(*aa["b"].get!(double*)); } Prints something like the following: ["b":7FFD0104B100] 7FFD0104B100 1.5 Ali |
February 09, 2016 Re: How do you reference variables in an AA of Variants? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Enjoys Math | On Tuesday, 9 February 2016 at 03:49:11 UTC, Enjoys Math wrote: > This: > double b = 1.0; > > Variant[string] aa = ["b": &b]; > > writeln(aa["b"]); > > fails with: > > Error: cannot implicitly convert expression(["b":&b]) of type double*[string] to VariantN!20u[string] > > Helps please! I've found bugbears like this are distressingly common in std.variant. Another one you might find yourself dealing with is https://issues.dlang.org/show_bug.cgi?id=10223, which applies to AAs as much as regular arrays. It's actually why I stopped using it in favour of Adam Ruppe's arsd.jsvar. -Wyatt |
Copyright © 1999-2021 by the D Language Foundation