www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Q: how to do AA of delegates keyed by int?

reply Brian Chapman <nospam-for-brian see-post-for-address.net> writes:
Help? I'm having trouble figuring out how to get a delegate out of an AA.

	void delegate() hash [int];
	
	int key = 5;
	hash[key] = delegate void() {printf ("whee!\n");};

Now how do I go about getting that value out?

	void delegate() fn = (key in hash);

doesn't work and neither does:

	void delegate()* fn = (key in hash);

which according to the docs I would think is how it is supposed to work 
seeing how it's supposed to return a pointer to the value type. But the 
compiler says:

"cannot implicitly convert expression key in hash of type int to void 
delegate()*"

I give up. What am I doing wrong?

Thanks in advance for putting up with my lameness. ;-)
Feb 01 2005
next sibling parent Carlos Santander <Carlos_member pathlink.com> writes:
In article <ctns13$8fu$1 digitaldaemon.com>, Brian Chapman says...
Help? I'm having trouble figuring out how to get a delegate out of an AA.

	void delegate() hash [int];
	
	int key = 5;
	hash[key] = delegate void() {printf ("whee!\n");};

Now how do I go about getting that value out?

	void delegate() fn = (key in hash);

doesn't work and neither does:

	void delegate()* fn = (key in hash);
This one works for me (dmd 0.111, winxp)
which according to the docs I would think is how it is supposed to work 
seeing how it's supposed to return a pointer to the value type. But the 
compiler says:

"cannot implicitly convert expression key in hash of type int to void 
delegate()*"

I give up. What am I doing wrong?

Thanks in advance for putting up with my lameness. ;-)
---------------- Carlos Santander
Feb 01 2005
prev sibling next sibling parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Brian Chapman wrote:
 Help? I'm having trouble figuring out how to get a delegate out of an AA.
 
     void delegate() hash [int];
     
     int key = 5;
     hash[key] = delegate void() {printf ("whee!\n");};
 
 Now how do I go about getting that value out?
Have you tried the 'normal' method? void delegate() fn = hash[key];
     void delegate() fn = (key in hash);
 
 doesn't work
Correct, as the InExpression should be of type pointer to delegate.
 and neither does:
 
     void delegate()* fn = (key in hash);
 
 which according to the docs I would think is how it is supposed to work 
 seeing how it's supposed to return a pointer to the value type. But the 
 compiler says:
 
 "cannot implicitly convert expression key in hash of type int to void a
 delegate()*"
<snip> This should work. It looks as if the in operator has a bug or two with determining its return type. Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Feb 01 2005
parent reply pragma <pragma_member pathlink.com> writes:
In article <cto6bg$kkb$1 digitaldaemon.com>, Stewart Gordon says...
 "cannot implicitly convert expression key in hash of type int to void a
 delegate()*"
<snip> This should work. It looks as if the in operator has a bug or two with determining its return type.
Actually, its not a bug. The 'in' operator is a 'boolean' operation ("does this key exist in the map?"), so it returns 'int' (true/false actually). Also, Stewart's suggestion of "hash[key]" is the correct approach, however it does have the side-effect of generating an empty value for 'key' (the AA grows as a result) if the key doesn't already exist in the map. The result is 'fn' gets assigned null, but future checks to "key in hash" will return true, not false. The side-effect free code is like this:
 void delegate fn;
 if(key in hash){
     fn = hash[key];
 }
- EricAnderton at yahoo
Feb 01 2005
next sibling parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
pragma wrote:

 Actually, its not a bug.  The 'in' operator is a 'boolean' operation ("does
this
 key exist in the map?"), so it returns 'int' (true/false actually).  
The return type of "in" was changed in DMD 0.107, from bit to pointer. http://www.digitalmars.com/d/changelog.html#new0107
 InExpressions now, instead of returning a bit, return a pointer to the
 associative array element if the key is present, null if it is not. This
 obviates the need for many double lookups.
http://www.digitalmars.com/d/arrays.html#associative
 The InExpression yields a pointer to the value if the key is in the
 associative array, or null if not:
 
 	int* p;
 	p = ("hello" in b);
 	if (p != null)
 		...
Not that D cares about the difference between false and null anyway. --anders
Feb 01 2005
parent reply pragma <pragma_member pathlink.com> writes:
In article <cto98f$njs$1 digitaldaemon.com>,
=?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= says...
pragma wrote:

 Actually, its not a bug.  The 'in' operator is a 'boolean' operation ("does
this
 key exist in the map?"), so it returns 'int' (true/false actually).  
The return type of "in" was changed in DMD 0.107, from bit to pointer. http://www.digitalmars.com/d/changelog.html#new0107
 InExpressions now, instead of returning a bit, return a pointer to the
 associative array element if the key is present, null if it is not. This
 obviates the need for many double lookups.
Well, crap. Sorry if I mislead anyone. :( I'll read the changelog a little more closely for now on! - EricAnderton at yahoo
Feb 01 2005
parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
pragma wrote:

The return type of "in" was changed in DMD 0.107, from bit to pointer.
 Well, crap.  Sorry if I mislead anyone. :(
 
 I'll read the changelog a little more closely for now on!
On the contrary, your "no side-effects" code still works - and seems to be the only one not affected by new "in" bugs ? "if (key in hash)" still works, just as "if (object)" does. --anders
Feb 01 2005
prev sibling next sibling parent "Regan Heath" <regan netwin.co.nz> writes:
On Tue, 1 Feb 2005 15:56:43 +0000 (UTC), pragma  
<pragma_member pathlink.com> wrote:
 In article <cto6bg$kkb$1 digitaldaemon.com>, Stewart Gordon says...
 "cannot implicitly convert expression key in hash of type int to void a
 delegate()*"
<snip> This should work. It looks as if the in operator has a bug or two with determining its return type.
Actually, its not a bug. The 'in' operator is a 'boolean' operation ("does this key exist in the map?"), so it returns 'int' (true/false actually).
Didn't this change? i.e. "What's New for D 0.107 Nov 29, 2004 ... InExpressions now, instead of returning a bit, return a pointer to the associative array element if the key is present, null if it is not. This obviates the need for many double lookups." http://www.digitalmars.com/d/arrays.html#associative "The InExpression yields a pointer to the value if the key is in the associative array, or null if not: int* p; p = ("hello" in b); if (p != null) ..." <snip> Regan
Feb 01 2005
prev sibling parent reply Brian Chapman <nospam-for-brian see-post-for-address.net> writes:
On 2005-02-01 09:56:43 -0600, pragma <pragma_member pathlink.com> said:
 
 The side-effect free code is like this:
 void delegate fn;
 if(key in hash){
 fn = hash[key];
 }
- EricAnderton at yahoo
Thanks Everyone! Walter's suspicion was correct. I was indeed using an older version (but yet reading the latest docs). I'm still using GDC-0.08/GCC-3.3.4 (on Mac OS X 10.2.8) because I couldn't get GDC-0.10 to work with GCC-3.3.4 which means I'm probably going to have to download the latest GCC. However, I live on a puny modem (56k that only connects at 28.8) and so I procrastinate downloading big files a lot, GCC being one of them. But yeah I should get it done nonetheless. Anyway, I was able to get it to work with Eric's side-effect free code. So thank you for that! I guess a little double-lookup won't hurt me till I pull in the new GCC/GDC. Thanks again guys. You big! Me small. ;-)
Feb 01 2005
parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Brian Chapman wrote:

 Walter's suspicion was correct. I was indeed using an older version (but 
 yet reading the latest docs). I'm still using GDC-0.08/GCC-3.3.4 (on Mac 
 OS X 10.2.8) because I couldn't get GDC-0.10 to work with GCC-3.3.4 
 which means I'm probably going to have to download the latest GCC. 
I'm using GDC-0.10 with GCC-3.3.5, but on the latest Mac OS X 10.3.7... Did you post the compiler errors you get on the digitalmars.D.gnu group?
 However, I live on a puny modem (56k that only connects at 28.8) and so 
 I procrastinate downloading big files a lot, GCC being one of them. But 
 yeah I should get it done nonetheless.
Can't you just order a CD ? Lots of places sell distfiles like gcc. Still have to get Mango 1.1 to compile with this compiler/platform, though. And of course, there's also the goodies from DMD 0.111/0.112 --anders
Feb 02 2005
parent Brian Chapman <nospam-for-brian see-post-for-address.net> writes:
On 2005-02-02 04:09:14 -0600, =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= 
<afb algonet.se> said:

 Brian Chapman wrote:
 
 Walter's suspicion was correct. I was indeed using an older version 
 (but yet reading the latest docs). I'm still using GDC-0.08/GCC-3.3.4 
 (on Mac OS X 10.2.8) because I couldn't get GDC-0.10 to work with 
 GCC-3.3.4 which means I'm probably going to have to download the latest 
 GCC.
I'm using GDC-0.10 with GCC-3.3.5, but on the latest Mac OS X 10.3.7... Did you post the compiler errors you get on the digitalmars.D.gnu group?
Well, you wont believe this but I went back to nail down the problem so I could tell you what it was and found out it was something really simple, all be it, very odd. Somehow a -Werror flag had creeped into my DFLAGS makefile variable. It was causing GDC to output a sparse object file and leaving a nice .s file in the working directory for each .d file compiled. Took that W flag out and it works fine. Put it back in and weirdness. You know, those GCC guys are never gonna take us seriously with that kind of goofiness going on. ;-)
 However, I live on a puny modem (56k that only connects at 28.8) and so 
 I procrastinate downloading big files a lot, GCC being one of them. But 
 yeah I should get it done nonetheless.
Can't you just order a CD ? Lots of places sell distfiles like gcc.
Yeah I used to do that. Place called Cheap Bytes. I don't know if they are still around. It's not so bad if I remember to stick wget on it before I go to bed. ;-)
 Still have to get Mango 1.1 to compile with this compiler/platform, 
 though. And of course, there's also the goodies from DMD 0.111/0.112
 
 --anders
Feb 03 2005
prev sibling parent "Walter" <newshound digitalmars.com> writes:
"Brian Chapman" <nospam-for-brian see-post-for-address.net> wrote in message
news:ctns13$8fu$1 digitaldaemon.com...
 I give up. What am I doing wrong?
I think you might be using an older DMD version. Try upgrading to 0.111.
Feb 01 2005