Smalltalk variable types

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

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

MyClass class
  instanceVariableNames: 'zzz'

      

+3


source to share


3 answers


An instance variable ( x

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

The class variable ( Yy

) is local to the class, all its instances, all subclasses and all subinstances (so the entire hierarchy). Any subclass or substance can see the value of this variable.



A class instance variable ( zzz

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

+10


source


variables are identifiers. The variable contains a reference to some object.

instanceVariableNames: This is where the x

instance of the class belongs.

classVariableNames: there Yy

is a copy of a variable shared by all instances of all classes, and it can be a static variable. therefore x

can have different meanings for different objects. But it Yy

can only have one meaning.



poolDictionaries: created in smallTalk to provide access to variables common to a group of classes

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

Class subclass

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

+3


source


To avoid confusion between non-smalltalkers: what you typed was a message (to the Object class) to ask it to subclass itself named "MyClass" with an instance variable (instance-private slot) named "x" and a class variable with the name "Yy" ". Next is a message to the newly created class to define an instance variable of the class (this is a slot in the class object, not in its instances) named" zzz ".

  • Global variables - "Object" and "MyClass" are "global variables". They are visible everywhere and are technical links in the global dictionary (holding key-value pairs). Older implementations only had one such dictionary; in later implementations there are several, and these are called "namespaces". In your example, the class definition message sent to the Object class will create such a new binding for the name MyClass.

  • Class variables - 'Yy' - class variable; this also applies to binding, but this binding is only visible within the class and its subclasses, both to class methods and instance methods (see below). All references are to the same binding, so subclasses will see the same value. They can be overridden in subclasses, but again, all subclasses of an override subclass refer to the same binding.

  • Instance variables are the private slots of an object. "x" is one in your example. The layout of the object and methods (operations) is inherited by subclasses, but of course each individual instance has a different meaning. Instance variables are visible to instance methods (of course).

  • Class Instance Variables, since classes are objects themselves (metaclass instances), they can have private slots as well. Technically, these are just instance variables of a class object and are visible to class methods. As with instance variables, layout and methods are inherited by subclasses, but each (class) has a different meaning. This is often misunderstood by C ++ / Java people, as they do not have a corresponding construct in their language (also remember that class methods are inherited and can be overridden in Smalltalk, whereas static functions in other languages ​​cannot)

  • Pool variables (shared pools) are like class variables in that they refer to bindings that are not globally held by a class object. However, they are visible in a number of interacting classes (similar to "friends" in another language). By naming a pool in a class definition message, its bindings become visible to the class and instance methods. They are usually used to define general constants.

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

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

0


source







All Articles