Thread overview
opAssign: cannot set reference of object to null
Apr 21, 2008
Mahe
Apr 21, 2008
Frits van Bommel
April 21, 2008
Hi,
in the following code the statement "b = null" calls "B opAssign( A v )" and causes an access violation. I don't understand why! How can I set b to null?
I'm using dmd 1.028.

class A
{
	this( int i )
	{
		a = i;
	}
	int a;
}

class B
{
	this( int i )
	{
		a = i;
	}
	int a;

	B opAssign( A v )
	{
		a = v.a;
		return this;
	}
}

int main(char[][] args)
{

	A a = new A( 4 );
	assert( a.a == 4 );
	B b ;
	assert( b is null );
	b = new B( 5 );
	b = a;
	assert( b.a == 4 );
	b = null;
	assert( b is null );
}
April 21, 2008
Mahe wrote:
> in the following code the statement "b = null" calls "B opAssign( A v )" and causes an access violation. I don't understand why! How can I set b to null?
> I'm using dmd 1.028.
[snip code]

Looks like a bug. Please report it here: http://d.puremagic.com/issues/
A workaround is probably to do "b = cast(B) null".
April 21, 2008
"Mahe" wrote
> Hi,
> in the following code the statement "b = null" calls "B opAssign( A v )"
> and causes an access violation. I don't understand why! How can I set b to
> null?
> I'm using dmd 1.028.
>
> class A
> {
> this( int i )
> {
> a = i;
> }
> int a;
> }
>
> class B
> {
> this( int i )
> {
> a = i;
> }
> int a;
>
> B opAssign( A v )
> {
> a = v.a;
> return this;
> }
> }
>
> int main(char[][] args)
> {
>
> A a = new A( 4 );
> assert( a.a == 4 );
> B b ;
> assert( b is null );
> b = new B( 5 );
> b = a;
> assert( b.a == 4 );
> b = null;
> assert( b is null );
> }

That is interesting... I know that you cannot overload opAssign with something that implicitly casts to the lvalue type, but what does the compiler do in the case of null?  It casts both to the lvalue type, and possibly the argument that is passed to opAssign.

IMO, it should not call opAssign, but set the implicit reference.  I'd file it as a bug.

You may be able to workaround by doing:

b = (B)null;

But that seems like a lot of extra work.

-Steve