Jump to page: 1 2
Thread overview
Q: how to do AA of delegates keyed by int?
Feb 01, 2005
Brian Chapman
Feb 01, 2005
Carlos Santander
Feb 01, 2005
Stewart Gordon
Feb 01, 2005
pragma
Feb 01, 2005
pragma
Feb 01, 2005
Regan Heath
Feb 01, 2005
Brian Chapman
Re: Q: how to do AA of delegates keyed by int? (Mac)
Feb 03, 2005
Brian Chapman
Re: how to do AA of delegates keyed by int?
Feb 01, 2005
Walter
February 01, 2005
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. ;-)

February 01, 2005
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
February 01, 2005
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.
February 01, 2005
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
February 01, 2005
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
February 01, 2005
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
February 01, 2005
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
February 01, 2005
"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.


February 01, 2005
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
February 01, 2005
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. ;-)

« First   ‹ Prev
1 2