. .

st4u

ST 4U 317: Dictionary Variance

December 5, 2012 7:32:02.809

Today's Smalltalk 4 You looks at a small behavior difference in the way things work in VA Smalltalk, Pharo, and VisualWorks, involving strings and IdentityDictionary. If you have trouble viewing it here in the browser, you can also navigate directly to YouTube. To watch now, click on the image below:

Identoty.

If you have trouble viewing that directly, you can click here to download the video directly. If you need the video in a Windows Media format, then download that here.

You can also watch it on YouTube:


Today we'll look at IdentityDictionary to point out a small difference in how things work in VA, Pharo, and VisualWorks. Consider the following code:


dict := Dictionary new.
dict 
	at: 'one' put: 1;
	at: 'two' put: 2;
	at: 'three' put: 3.
^dict at: 'one'.

You won't be surprised by the answer in any Smaltalk. Now consider this:


dict := IdentityDictionary new.
dict 
	at: 'one' put: 1;
	at: 'two' put: 2;
	at: 'three' put: 3.
^dict at: 'one'.

In Pharo and VA, you get back 1 - the literals are apparently reused, making them safe as keys in an IdentityDictionary. In VisualWorks, on the other hand, you get a KeyNotFound exception. In VisualWorks, the literals are not reused (and, since the introduction of immutability a few releases ago, that behavior is guaranteed).

If you are working on a project that is maintaining code across multiple Smalltalk implementations, this is a useful thing to know.

Need more help? There's a screencast for other topics like this which you may want to watch. Questions? Try the "Chat with James" Google gadget over in the sidebar.

Tags: ,

Enclosures:
[st4u317-iPhone.m4v ( Size: 2339597 )]

posted by James Robertson

 Share Tweet This