Variable types in smalltalk

2.2k views Asked by At

I need help understanding the usage and the difference of variables in Smalltalk. What is the difference and the usage of each variable in the given code below?

Object subclass: #MyClass
  instanceVariableNames: 'x'
  classVariableNames: 'Yy'
  poolDictionaries: ''
  category: 'helpMe'

MyClass class
  instanceVariableNames: 'zzz'
3

There are 3 answers

2
Max Leske On BEST ANSWER

An instance variable (x) is a variable that is local to an instance. Neither the class nor any other instance can access that variable.

A class variable (Yy) is local to a class, all its instances, all subclasses and all subinstances (so the entire hierarchy). Any subclass or subinstance can see the value of that variable.

A class instance variable (zzz) is local to a class. Only the class that defines the variable has access to it, neither instances nor subclasses can see the variable (although subclasses inherit the declaration of the variable, their variable will have a different value). Classes are also objects in Smalltalk. So you can think of a class instance variable the same way you would think about an instance variable: no other instance (instance of a class) can see the value. Thanks to @Amos M. Carpenter for pointing this out.

0
NIMISHAN On

variables are identifiers. A variable holds a reference to some object.

instanceVariableNames: here x belongs to an instance of a class.

classVariableNames: here Yy have a copy of the variable shared with all instances of all classes, and it can be static a variable. so x can have different values across different objects. But Yy can have only one value.

poolDictionaries: are created in smallTalk to provide access to variables shared among a group of classes

category 'helpme' is a collection of related classes, if you create your class without a category; the class gets created with a blank category.

subclass has its own instanceVariableNames(zzz), also has the inheritance property.

0
blabla999 On

To avoid confusion among non-smalltalkers: what you typed in was a message (to the Object class) to ask it to create a subclass of itself, named 'MyClass', with an instance variable (an instance-private slot) named 'x' and a class variable named 'Yy'. Followed by a message to that just created class to define a class instance variable (that is a slot in the class object - not in instances of it) named 'zzz'.

  • Global variables
    'Object' and 'MyClass' are "global variables". These are visible everywhere and are technically bindings in a global Dictionary (holding key-value pairs). In old implementations, there was only one such dictionary; in more recent implementations, there are multiple, and they are called "namespaces". In your example, the class-definition message as sent to the Object class will create such a new binding for the 'MyClass' name.

  • Class variables
    'Yy' is a class variable; this also refers to a binding, but that binding is only visible inside the class and its subclasses, both to class methods and instance methods (see below). All referencing the same binding, thus subclasses will see the same value. They can be redefined in subclasses, but then again, all subclasses of the redefining subclass refer to the same binding.

  • Instance variables
    these are private slots of an object. 'x' is one in your example. The layout of an object and methods (operations) are inherited by subclasses, but of course, each individual instance has its own value. Instance variables are visible to instance methods (of course).

  • Class Instance Variables
    because classes are themself objects (instances of a meta-class), these can have private slots as well. Technically, they are simply instance variables of the class object, and visible to class methods. As with instance variables, the layout and methods are inherited by subclasses, but each (class) has its own value. This is often misunderstood by C++/Java people, as they have no corresponding construct in their language (also be aware that class methods are inherited and can be redefined in Smalltalk, whereas static functions in other languages cannot)

  • Pool Variables (Shared Pools)
    are similar to class variables, in that they refer to bindings which are non-globally held by a class object. However, these are visible in a number of collaborating classes (similar to "friends" in another language). By naming a pool in the class definition message, its bindings become visible to the class and instance methods. Usually, these are used to define shared constants.

The other variable types (not present in your code example) are method-locals and block locals. These refer to slots in the current context, which is technically the stack-frame (continuation) of a method or a block (or an outer context, in case the block is actually a closure).

The 'category' is just an attribute of the class, not to be confused with the above variables. Think of it as a tag attached to it, to allow for a nicer organization in the browser. The details of where this attribute is stored is dialect specific: most use a separate (so-called "organization"), which is a dictionary. However, at least one dialect (ST/X) keeps it as a private slot of the class object (and there, it is really a variable in some sense).