opAssign and references
Timon Gehr
timon.gehr at gmx.ch
Tue Jan 31 08:31:22 PST 2012
On 01/31/2012 03:03 PM, Nicolas Silva wrote:
> 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)
Works for me. Which version of the compiler are you using?
> 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)
This is http://d.puremagic.com/issues/show_bug.cgi?id=7019
> }
>
>
> 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.
If something is not as simple and intuitive as it ought to be, there
might be a compiler bug.
>
> Best regards,
>
> Nicolas Silva
More information about the Digitalmars-d-learn
mailing list