8

In languages like Java and C++ we give parameters to constructors.

How do you do this in Pharo Smalltalk?

I want something like

|aColor|
aColor = Color new 'red'.

Or is this bad practice and should I always do

|aColor|
aColor = Color new.
aColor name:= red.d
Jeremy Knees
  • 652
  • 1
  • 7
  • 21

2 Answers2

12

The short answer is that you can do pretty much the same in Smalltalk. From the calling code it would look like:

aColor := Color named: 'Red'.

The long answer is that in Smalltalk you don't have constructors, at least not in the sense that you have a special message named after the class. What you do in Smalltalk is defining class-side messages (i.e. messages understood by the class, not the instance[*]) where you can instantiate and configure your instances. Assuming that your Color class has a name instance variable and a setter for it, the #named: method would be implemented like:

(class) Color>>named: aName
| color |
color := self new.
color name: aName.
^color.

Some things to note:

  • We are using the #new message sent to the class to create a new instance. You can think of the #new message as the primitive way for creating objects (hint: you can browse the implementors of the #new message to see how it is implemented).
  • We can define as many class methods as we want to create new 'configured' instances (e.g. Color fromHexa:) or return pre-created ones (e.g. Color blue).
  • You can still create an uninitialized instance by doing Color new. If you want to forbid that behavior then you must override the #new class message.

There are many good books that you can read about Smalltalk basics at Stef's Free Online Smalltalk Books

[*] This is quite natural due to the orthogonal nature on Smalltalk, since everything (including classes) is an object. If you are interested check Chapter 13 of Pharo by Example or any other reference to classes and metaclasses in Smalltalk.

HTH

Andrés Fortier
  • 1,705
  • 11
  • 12
  • 1
    Even better is not having any setters, and a single initialization method parameterised as needed: `Color >> #initializeWithName:` in this case. – Frank Shearar Jan 09 '13 at 08:38
  • 1
    To clarify a bit, the class is an object, the instance is another object. To respect encapsulation, the class has no access to instance variables of the instance - except by kindly asking thru message send. It's pretty uniform, Smalltalk is about sending messages, even for such basic kernel tasks... As FrankShearar said, it might be better to have a single message at instance side that set all variables at once, it's up to programmers to judge. – aka.nice Jan 09 '13 at 09:47
3

In Smalltalk all member fields are strictly private and to assign to them you'll have to define assigning methods.

Color >> name: aString
  name := aString

Then you could create your object like this:

|aColor|
aColor := (Color new)
  name: 'red';
  yourself.

Commonly to reduce verbosity factory method is used:

Color class >> withName: aName
  ^ (self new)
    name: aName;
    yourself.

With this you could create new objects like this:

|aColor|
aColor := Color withName: 'red'.
Nekuromento
  • 2,147
  • 2
  • 16
  • 17