Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smalltalk variables: why should I declare them?

Basically I can use variables just by assigning something to them, for example:

x := something

It works fine.

But in classes, if I define a new method, but I don't declare the variable, I get an "assignment to undeclared variable x", so I have to use:

|x| x := something

Why is this?

like image 448
eva02 Avatar asked Feb 25 '17 17:02

eva02


People also ask

Why do variables need to be declared?

The main purpose of variable declaration is to store the required data in the memory location in the form of variables so that we can use them in our program to perform any operation or task. By declaring a variable, we can use that variable in our program by using the variable name and their respective data type.

Should all variables be declared?

All variables must be declared before they can be used.

Do you need to declare and out variables before use it?

You can declare a variable in a separate statement before you pass it as an out argument. The following example declares a variable named number before it is passed to the Int32. TryParse method, which attempts to convert a string to a number.

Should you declare all variables at the beginning?

It's a good programming practice to declare all variables at the beginning of a script.


1 Answers

As Uko mentions, there are different kinds of variables in Smalltalk and that's why we need to declare them differently. Let's review all of Smalltalk variables here for the sake of completeness.


Instance variables


Meaning: These are the ones that define the shape of the class. For example, the class Point defines the ivars x and y.

Declared: In the class definition.

Scope: Local, restricted to every instance.

Case: usually lowercase.


Class variables


Meaning: These variables are shared among several objects. They are not global though.

Declared: In the class definition.

Scope: Shared by the class, its metaclass, all subclasses, their metaclasses, all instances and all subinstances.

Case: usually Uppercase.


Class instance variables


Meaning: Are instance variables of the metaclass and therefore determine the shape of the class (not of its instances).

Declared: In the class definition.

Scope: Local, restricted to class-side methods.

Case: usually lowercase.


Temporaries


Meaning: These are auxiliary variables used in methods and blocks. They get allocated in the execution stack every time the method is activated (expect for those declared in a method or block closure and used in an inner block).

Declared: Between pipes as in | temp | at the beginning of the method's (or block) body.

Scope: Activation of the method or block.

Case: usually lowercase.


Pool variables


Meaning: Are held in global PoolDictionaries and can be used in any class that declares their use.

Declared: The class declares the usage of the PoolDictionary that defines them.

Scope: The class and all subclasses and metaclasses.

Case: usually Uppercase.


Global variables


Meaning: Are shared by every object in the system.

Declared: Nowhere. They are defined in the Smalltalk system dictionary by meas of Smalltalk at: <GlobalSymbol> put: <an object>. Once declared their value can be changed using :=.

Scope: The entire image.

Case: usually Uppercase.

Remark: Class names are global variables. However, you should not assign them using := (unless you know what you are doing.)


Pseudo variables


Meaning: They can be read but not written. These are: self, super, true, false and nil.

Declared: Nowhere.

Scope: Can be used everywhere. However, the meaning of self and supper changes with the object that uses them.


Method and Block Arguments


Meaning: They represent the formal arguments that the method or block will accept (and require) at every activation.

Declared: In the method signature or the beginning of the block.

Scope: The method or block that declares them.

Case: lowercase.


Workspace variables


Meaning: These are variables associated to workspaces (Playground in Pharo).

Declared: Usually not explicitly declared (the workspace will declare them behind the scenes).

Scope: The life of the workspace.

Case: lowercase.

like image 135
Leandro Caniglia Avatar answered Sep 30 '22 12:09

Leandro Caniglia