Category:Smalltalk: Difference between revisions

Line 348:
 
===Variables===
All variables are actually 'bindings' of a name to a value (technically: holding a pointer to an object). Thus, the lifetime of an object is not related to the scope or lifetime of such a binding; although the VM's garbage collector will eventually free the underlying object, when no more binding or other reference exists. This is similar to eg. Java or JavaScript and different from eg. C++, where the scope may or may not imply construction/destruction of an object.
 
====Globals / Namespaces====
By convention, global and namespace variables are to be used for classes only. As in every other language, it is considered extremely bad style to use globals for anything else.
They are created by telling the namespace to add a binding, as in <lang smalltalk>Smalltalk at:#Foo put:<something></lang>. The binding's value can be retrieved by asking the namespace: <lang smalltalk>x := Smalltalk at:#Foo</lang> or simply by referring to the binding by name: <lang smalltalk>x := Foo</lang>.
As seen above, the global named <tt>Smalltalk</tt> refers to the Smalltalk namespace, which is globally visible.
====Class Variables (Statics)====
These are visible inside a class and shared with all of its subclasses. They have the lifetime of the defining class. The binding is shared with all subclasses. Typically, these are used for constants. Class variables are defined in the class definition message when the class is instantiated or redefined by setters to the class.
====Instance Variables====
These are the slots where the private state of an object is held. Instances have the same structure (layout), but each has its own values. The instance layout is defined in the class definition message when the class is instantiated or redefined by setters to the class.
====Class Instance Variables====
This is something not known in most other languages: these are per-class instance variables. be reminded that classes are also objects, and have their own private state. I.e. like with instance variables, the layout is shared with subclasses, but each subclass has its own values. This is typically used to hold on singletons, private per class caches, or private per class redefinable constants. The class instance layout is defined in the class definition message when the class is instantiated or redefined by setters to the class's meta class.
====Method Arguments and Locals====
These are visible inside a single method and all of its enclosed blocks (lambda closures).
Args are specified in the method's first definition line, locals are defined by listing the names between vertical bars at the beginning of a method:<lang smalltalk>| varA varB ... |</lang>
====Block Arguments and Locals====
This are visible inside a block and all of its enclosed blocks. Args are specified by listing them prefixed by a colon, following a vertical bar, followed by optional local variables:
<lang smalltalk>[:arg | ... block's code here...]. "a one-arg block"
[:a1 :a2 :a3 | ... ] "a three-arg block"
[ |local1 local2| ... ] "a no-arg block with 2 locals"
[:a :b :c | | l1 l2 l3 | "3 args (a,b,c) and three locals (l1,l2,l3)"</lang>
Blocks can be nested, and inner blocks can refer to any statically visible outer scope's variable.
====Pool Dictionaries====
Are additional namespaces (name-value bindings), which are explicitly "imported" to make all their bindings visible. Used to share constants or singletons (or even classes) between cooperating classes, which are not inheriting from a common superclass.
====Private Classes====
Not supported by all dialects (but can be simulated with namespaces or pool dictionaries). These are bindings to classes which are only locally visible.
 
Be reminded that all those bindings are dynamic structures, which can be created or changed at run time. This makes it trivial to generate code on the fly, add domain specific languages, anonymous or super private classes etc.
 
Also, the mapping from method names (called "message selectors") to code (called "methods") is also held in a dictionary inside classes. This can also be changed dynamically or reflected upon by asking the class for a corresponding binding.
 
===Special "builtin" Pseudo Variables===
Anonymous user