opAssign and references
Nicolas Silva
nical.silva at gmail.com
Tue Jan 31 06:03:44 PST 2012
Hi,
I'm playing with variants and I noticed that opAssign is not invoked
when an assignation is done on a reference.
here is the test case:
import std.variant;
struct Foo
{
Variant a;
Variant b;
ref Variant refA()
{
return a;
}
}
void main()
{
Foo f1;
f1.a = 42; // ok
f1.b = 23; // ok
f1.refA() = 24; // Error: cannot implicitly convert expression
(24) of type int to VariantN!(maxSize)
f1.refA().opAssign( 24 ); // ok, but not very nice...
// slightly OT but
Foo f2 = { a: 10 }; // Error: cannot implicitly convert expression
(10) of type int to VariantN!(maxSize)
}
Is it normal? Am I missing something?
I took Variant as an example because it does use opAssign but one
could create a struct defining opAssign with the same results.
More generally, I feel like I don't really understand the semantic of
opAssign (or maybe references). I'd intuitively expect it to be
invoked when the "=" operator is used on a reference and I'd also
expect it to be invoked in struct initializers (though there might be
something about compiletime / runtime stories in this particular
case, yet i think one would expect it to just work).
D aims at being simple and intuitive, and in this case it looks not so
intuitive to me.
Best regards,
Nicolas Silva
More information about the Digitalmars-d-learn
mailing list