views:

124

answers:

3

What is the Smalltalk equivalent of Java's static fields and methods? IOW, what do the Smalltalkers do when they need class level data and/or methods?

+7  A: 

We use class-side methods/instance variables. A class is an object, after all, so can have methods.

For instance, the Rectangle class has a method #origin:corner: so you may write

Rectangle origin: 0@0 corner: 100@100

to create a Rectangle. This is just the message #origin:corner: sent to the object called Rectangle (a class is an object!) with the two Points as parameters.

Class-side instance variables work much the same way. A class, being an object, may have instance variables. From the SUnit library:

TestCase class
  instanceVariableNames: 'history'

and then TestCase class exposes this in the usual way, with a getter/setter (#history and #history:).

EDIT: The @ I used has caused a fair bit of discussion. It's what's called a binary message, which allows one to define selectors that look just what other languages would call infix operators. For instance, 3 + 4, or 0@0. In the case of @, the Number class defines a method called @ taking a parameter y, defined as ^Point x: self y: y - "return a Point whose x coordinate is my own value and whose y coordinate is the parameter".

Point is an ordered pair, but of course there's nothing stopping one from defining higher-dimensional versions. Point might define a method called @ that looked like this, for instance: ^Triple x: self x y: self y z: z - "return a point in R^3 whose x, y coordinates are my own, and whose z coordinate is the given parameter".

Frank Shearar
Do any languages other than Smalltalk use @ to delimit the different coordinates of a point? I don't believe I've seen that specific syntax before.
JAB
Does that `@` indicate a tuple?
Jim
@ is a "binary message" - a message that takes a single parameter, but doesn't need the usual trailing :. Number defines a method called @ that returns a Point: ^Point x: self y: y (where y is the sole parameter, obviously).
Frank Shearar
+1  A: 

In most types of Smalltalk you have class variables. Typically class variables are used for e.g. singletons. There are differences in the types of variables that you can use between flavors of Smalltalk though, so read the documentation on this point for your particular implementation.

iwein
+1  A: 

The most important mind-shift needed if you come to Smalltalk from Java or such, is that classes are objects.

A static in Java-like languages can have many different semantics. Usually this has to do with visibility. You need an object that is independent from any instances of a class, but you want to restrict the visibility of this object to within the class, that is: only visible from instances of the class or the class itself (in Smalltalk, because in Java classes are not first-class objects).

In Smalltalk you usually have more options for this:

  1. Class instance variables
  2. Class variables or pool variables (depending on your Smalltalk dialect)

A class instance variable is indeed just like an instance variable of instances of any class: the class has this property, and this can be made accessible for any instance of the class by providing a getter method on the class (not on the instances, we call this a class method). This is useful if you have default values and such. Example:

Define a class Car, with instance variable colour, PLUS a class instance variable defaultColour (which of course would have the value "BLACK" ;-))

Smalltalk defineClass: #Car
    superclass: #{Core.Object}
    indexedType: #none
    private: false
    instanceVariableNames: 'colour '
    classInstanceVariableNames: 'defaultColour'
    imports: ''
    category: ''

This is a class definition (actually a message to the object Smalltalk) in VisualWorks Smalltalk. If you create a subclass of Car, it inherits the class instance variable defaultColour, as a normal object would do as well. If the defaultColour class instance variable has a value, the subclass also inherits this value!

robject