www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How do I call super or object.opAssign for classes?

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
class Foo
{
    void opAssign(int bar)
    {
    }
}

void main()
{
    auto foo = new Foo;
    foo = null;
}

test.d(17): Error: function test.Foo.opAssign (int bar) is not
callable using argument types (void*)
test.d(17): Error: cannot implicitly convert expression (null) of type
void* to int

I just wanted to implement one opAssign method for assigning a
specific type, but now I've ran into the issue that assigning class
objects to null doesn't work anymore.. :/
Jul 23 2011
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Sat, 23 Jul 2011 12:23:07 -0400, Andrej Mitrovic  
<andrej.mitrovich gmail.com> wrote:

 class Foo
 {
     void opAssign(int bar)
     {
     }
 }

 void main()
 {
     auto foo = new Foo;
     foo = null;
 }

 test.d(17): Error: function test.Foo.opAssign (int bar) is not
 callable using argument types (void*)
 test.d(17): Error: cannot implicitly convert expression (null) of type
 void* to int

 I just wanted to implement one opAssign method for assigning a
 specific type, but now I've ran into the issue that assigning class
 objects to null doesn't work anymore.. :/
That's a bug, please file. -Steve
Jul 25 2011
next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6378

On 7/25/11, Steven Schveighoffer <schveiguy yahoo.com> wrote:
 On Sat, 23 Jul 2011 12:23:07 -0400, Andrej Mitrovic
 <andrej.mitrovich gmail.com> wrote:

 class Foo
 {
     void opAssign(int bar)
     {
     }
 }

 void main()
 {
     auto foo = new Foo;
     foo = null;
 }

 test.d(17): Error: function test.Foo.opAssign (int bar) is not
 callable using argument types (void*)
 test.d(17): Error: cannot implicitly convert expression (null) of type
 void* to int

 I just wanted to implement one opAssign method for assigning a
 specific type, but now I've ran into the issue that assigning class
 objects to null doesn't work anymore.. :/
That's a bug, please file. -Steve
Jul 25 2011
prev sibling parent reply =?ISO-8859-1?Q?Diego_Canuh=E9?= <canuhedc gmail.com> writes:
Hi,
isn't that the way it's supposed to work? I mean

void show(int a) { writeln(a); }

void main() { show(null); }

won't compile either.
Shouldn't bar be some kind of pointer?

btw, today I read "opAssign can no longer be overloaded for class objects"
here:

http://www.d-programming-language.org/features2.html

is that no longer valid?
Jul 26 2011
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 26 Jul 2011 07:54:54 -0400, Diego Canuh=C3=A9 <canuhedc gmail.co=
m>  =

wrote:

 Hi,
 isn't that the way it's supposed to work? I mean

 void show(int a) { writeln(a); }

 void main() { show(null); }

 won't compile either.
 Shouldn't bar be some kind of pointer?
null should be considered as the type of the class, not void *. You = should not be able to override the behavior of opAssign as it pertains t= o = assigning to a class instance. In other words, you should *never* be ab= le = to override x =3D null where x is a class instance.
 btw, today I read "opAssign can no longer be overloaded for class  =
 objects"
 here:

 http://www.d-programming-language.org/features2.html

 is that no longer valid?
Here is the spec. I would not trust that features2, it's probably out o= f = date. http://www.d-programming-language.org/operatoroverloading.html#Assignmen= t -Steve
Jul 26 2011