Send to Printer


ST 4U 345: instVarAt:

February 15, 2013 10:14:56.721

Today's Smalltalk 4 You looks at one of the low level API aspects of Smalltalk - #instVarAt: and #instVarAt:put:. 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:


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 a way you can break encapsulation in Smalltalk - useful for some low level tasks (interfacing with databases, for instance) - but dangerous in general. Still, it's useful to know what you can accomplish, so long as you are also aware of the dangers. Consider this class:

Object subclass: #Person
    classInstanceVariableNames: 'nextId '
    instanceVariableNames: 'id first last '
    classVariableNames: ''
    poolDictionaries: ''

Now consider this code:

person := Person new.
person first: 'James'.
person last: 'Robertson'.

person instVarAt: 1

Execute that, and you should get back the value in 'id' = the first instance variable. Again this is not something you want to make use of on a normal basis. It can be useful when debugging (say you have a class without accessing methods), or when creating accessing frameworks for external data. Similarly:

person instVarAt: 1 put: 0.

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: , ,

[st4u345-iPhone.m4v ( Size: 1182765 )]

posted by James Robertson

 Share Tweet This