opAssign() still accepted for classes???

KennyTM~ kennytm at gmail.com
Fri Apr 29 10:49:19 PDT 2011


On Apr 30, 11 01:36, Steven Schveighoffer wrote:
> On Fri, 29 Apr 2011 13:29:56 -0400, KennyTM~ <kennytm at gmail.com> wrote:
>
>> On Apr 30, 11 01:19, Steven Schveighoffer wrote:
>>> I think he was referring to the line:
>>>
>>> X x = 0;
>>>
>>> Where x could not possibly be anything other than null.
>>>
>>> -Steve
>>
>> That's definitely a bug. Why should a declaration call opAssign?
>
> X x = new X;
>
> is equivalent to
>
> X x;
> x = new X;
>
> new X is an expression, which doesn't have to be the rvalue of an
> assignment, I suppose you can substitute any valid expression for it:
>
> X x;
> x = 0;
>
> =>
>
> X x = 0;
>
> Also, if X is a struct, and the struct defines opAssign(int), this would
> be valid.
>
> I wouldn't mind if it was a bug, because clearly you never want to call
> opAssign on an uninitialized class. But it definitely would be a special
> case.
>
> -Steve

I see.

Though this isn't valid with a 'struct'.

------------------------
import std.stdio;
struct K {
     K opAssign(int x) {
         writeln(x);
         return this;
     }
}
void main() {
     // K k = 8;   // Error: cannot implicitly convert expression (8) of 
type int to K
     K k;
     k = 8;        // OK.
}
------------------------







More information about the Digitalmars-d mailing list