Thread overview
nullifying delegates
Sep 20, 2003
Lars Ivar Igesund
Sep 20, 2003
Matthew Wilson
Sep 20, 2003
Lars Ivar Igesund
Sep 20, 2003
Mike Wynn
Sep 21, 2003
Mike Wynn
Sep 25, 2003
Walter
Sep 25, 2003
Dario
September 20, 2003
How do I nullify a delegate in an easy way?

I have an array of delegates and want to have
the possibility to remove some of them.

dg = null;
don't work:
cannot implicitly convert void* to void delegate(Foo)

I can take the address of a dummy static function,
but that seems silly. I guess it will work if I assign
an uninitialized delegate to it, but I really want to
use null.

Lars Ivar Igesund


September 20, 2003
Does a cast (from 0 or null) not work?

"Lars Ivar Igesund" <larsivi@stud.ntnu.no> wrote in message news:bkidhp$2lpr$1@digitaldaemon.com...
> How do I nullify a delegate in an easy way?
>
> I have an array of delegates and want to have
> the possibility to remove some of them.
>
> dg = null;
> don't work:
> cannot implicitly convert void* to void delegate(Foo)
>
> I can take the address of a dummy static function,
> but that seems silly. I guess it will work if I assign
> an uninitialized delegate to it, but I really want to
> use null.
>
> Lars Ivar Igesund
>
>


September 20, 2003
> How do I nullify a delegate in an easy way?

This should do it:

delete dg;



September 20, 2003
"Matthew Wilson" <matthew@stlsoft.org> wrote in message news:bkie1g$2me6$1@digitaldaemon.com...
> Does a cast (from 0 or null) not work?
>

Nope;

e2ir: cannot cast from int to void delegate(Foo)
e2ir: cannot cast from void* to void delegate(Foo)

I guess
delete dg;
might work, but it feels wrong as I don't
new
the delegate.

Another thing; how do I check if the delegate is null,
zero, empty or whatever?
Hmm, seems like !dg might work.

Lars Ivar Igesund


September 20, 2003
Julio César Carrascal Urquijo wrote:
>>How do I nullify a delegate in an easy way?
> 
> 
> This should do it:
> 
> delete dg;
> 
> 

I would check what you post! that will cause a seg fault, delete is for removing items from a hashtable/assoc array.

I tried ...
import c.stdio;
alias int delegate( int ) iid;
class foo {
	int bar(int a){ return a;}
}

int evoke( iid func ) {
	if ( func ) {
		return func( 3 );
	}
	return 0;
}

void nulldel( out iid ignore ) {
}

int main( char[][] args ) {
	iid fb;
	printf( "delegate = null (0)\n" );
	printf( "delegate != null (3)\n" );
	printf( "fb clr %d\n", evoke( fb ) );
	fb = &((new foo()).bar);
	printf( "fb set %d\n", evoke( fb ) );
	nulldel( fb );
	printf( "fb clr %d\n", evoke( fb ) );
	return evoke( fb );
}

---------------------
but has no effect!! (Walker, I assume this it a bug out should init to null/0)
-------------------
however ... this workaround works.
import c.stdio;
alias int delegate( int ) iid;
class foo {
	int bar(int a){ return a;}
}

int evoke( iid func ) {
	if ( func ) {
		return func( 3 );
	}
	return 0;
}

void nulldel( out iid ignore ) {
	iid fake;
	ignore = fake;
}

int main( char[][] args ) {
	iid fb;
	printf( "delegate = null (0)\n" );
	printf( "delegate != null (3)\n" );
	printf( "fb clr %d\n", evoke( fb ) );
	fb = &((new foo()).bar);
	printf( "fb set %d\n", evoke( fb ) );
	nulldel( fb );
	printf( "fb clr %d\n", evoke( fb ) );
	return evoke( fb );
}



September 21, 2003
I did tried that before posting using DMD 0.73 on windows. I wrote this small program:

void main()
{
 Test t = new Test();
 void delegate(char[]) printer = &t.print;

 printer("Hello, world!\n");
 printf("printer = %p\n", printer);

 delete printer;
 printf("printer = %p\n", printer);
}


class Test
{
 void print(char[] message)
 {
  printf(message);
 }
}

That works just fine and writes:

Hello, world!
printer = 00690FD0
printer = 00000000

With no segmentation fault.


> I would check what you post! that will cause a seg fault, delete is for removing items from a hashtable/assoc array.

Delete is also used to delete pointers/references wich is what I did (remove a pointer to a member function).



September 21, 2003
Julio César Carrascal Urquijo wrote:
> I did tried that before posting using DMD 0.73 on windows. I wrote this
> small program:
> 
> void main()
> {
>  Test t = new Test();
>  void delegate(char[]) printer = &t.print;
> 
>  printer("Hello, world!\n");
>  printf("printer = %p\n", printer);
> 
>  delete printer;
>  printf("printer = %p\n", printer);
> }
> 
> 
> class Test
> {
>  void print(char[] message)
>  {
>   printf(message);
>  }
> }
> 
> That works just fine and writes:
> 
> Hello, world!
> printer = 00690FD0
> printer = 00000000
> 
> With no segmentation fault.
> 
> 
> 
>>I would check what you post! that will cause a seg fault, delete is for
>>removing items from a hashtable/assoc array.
> 
> 
> Delete is also used to delete pointers/references wich is what I did (remove
> a pointer to a member function).
> 

but it does not do as you expect ....

import c.stdio;

class Test {
	int b;
	this() {b = 100;}
	int func(int a) {
	  printf( "test[%d]::func(%d)\n", b, a );
	  return a+b;
	}
}

alias int delegate(int) memfunc;

int main( char[][] args )
{
	int a;
	memfunc printer;
	
	if ( printer ) {
		printf("b4: printer is not null\n" );
	} else {
		printf("b4: printer is null\n" );
	}
	
	printer = &((new Test()).func);

	a = printer( 22 );
	if ( printer ) {
		printf("set: printer is not null\n" );
	} else {
		printf("set: printer is null\n" );
	}

	delete printer;
	
	if ( printer ) {
		printf("post delete: printer is not null\n" );
	} else {
		printf("post delete: printer is null\n" );
	}
	
	return 0;
}

outputs .... (linux dmd)
> b4: printer is null
> test[100]::func(22)
> set: printer is not null
> post delete: printer is not null

(I changed the delegate to int(int) 'cos the delete seggy'd when I first  tried it, can't get it do do that again, forgot what code I actually wrote).





September 25, 2003
"Lars Ivar Igesund" <larsivi@stud.ntnu.no> wrote in message news:bkidhp$2lpr$1@digitaldaemon.com...
> How do I nullify a delegate in an easy way?
>
> I have an array of delegates and want to have
> the possibility to remove some of them.
>
> dg = null;
> don't work:
> cannot implicitly convert void* to void delegate(Foo)

That should work. I'll fix it. -Walter


September 25, 2003
Lars Ivar Igesund:
> How do I nullify a delegate in an easy way?
>
> I have an array of delegates and want to have
> the possibility to remove some of them.
>
> dg = null;
> don't work:
> cannot implicitly convert void* to void delegate(Foo)

You can use "dg = dg.init" until Walter fix the bug.