Syntax for class and instance variables and methods in Pharo 4.0

3.4k views Asked by At

I am learning Pharo online and am not sure if I got the syntax correct for creating class and instance variables. Please correct me if I am wrong :-

Class (Static) method created on class side of Pharo, where name, email, phone are instance variables of class CreateUser:

createNewUser:Arguments name:userName email:userEmail phone:userPhone

To call this static method of class CreateUser, I will do the following :-

CreateUser 
     name:userName
     email:userEmail
     phone:userPhone

If I want to create an instance variable by this name, the method declaration will be exactly the same as above, but it will be on the instance side of the class. However, when I call the method, I will call it using the keyword "new" to create a new instance as under:

CreateUser new
     name:userName
     email:userEmail
     phone:userPhone

When I run the above code and call this method statically, I get an error message as:-

MessageNotUnderstood: CreateUser class >>name:email:phone:

However, when I go to the CreateUser class to recheck, I see the above method create on the class side as :

CreateUser:name:email:phone:

My queries are as below: 1. What am I doing wrong above? How can I fix the above error? 2. The concept behind using static variables/methods vs class variables/methods is the same as Java? 3. If I wish to access the above instance variables, I can add accessor methods for class/instance and then call them with the class instance/class object instance. Is that correct?

Any help you can give will be greatly appreciated! Thanks very much in advance.

2

There are 2 answers

4
Uko On BEST ANSWER

I guess you misunderstand the method syntax a bit, because createNewUser:Arguments part makes no sense. What you should have is a method on the class side like this:

name: userName email: userEmail phone: userPhone
   "and here you probably have something like:"
   name := userName.
   email := userEmail.
   "and so on"

In my example name:email:phone: is the method's selector and userName, userEmail and userPhone are parameters. You can call this method as in your example. name and email are either class side on instance side variables depending on where the method is defined.

Also you shouldn't name a class CreateUser. Think about this, what will be the instances called? "createUsers"? Usually you name a class User, then you can think about instances as "users", and then the responsibility of the class object is "to create users (its instances)".

Please note, that it's weird to have a method like this on the class side. What you usually do is to create an instance method:

initializeName: userName email: userEmail phone: userPhone
   name := userName.
   email := userEmail.
   phone := userPhone

and a class side method:

newName: userName email: userEmail phone: userPhone
   | instance |
   instance := self new.
   instance initializeName: userName email: userEmail phone: userPhone.
   ^ instance

or a shorter version using cascaded messages:

newName: userName email: userEmail phone: userPhone
   ^ self new
      initializeName: userName email: userEmail phone: userPhone;
      yourself

2) In Pharo (and Smalltalk) the concept is a bit simpler. As everything is an object, Class is an object also, so class side variables and methods are instance variables and methods of a class which is an instance of the "class class". Next picture may help you to understand the associations between objects in Pharo:

enter image description here

This may be a bit confusing at the beginning, but in the end, you don't have and static/nonstatic methods/variables, you just have objects, instantiation and inheritance.

So what you should think about is what are you going to ask the objects about. Probably you should ask User about it's email or mobile number, but you will ask User class to create a user or find a user, or suggest a default t-shirt size for a user.

3) Yes, you should make an accessor. Moreover, if you select a class in system browser and press Cmd+H+A (or Ctrl, or Alt) depending on your OS, you will get a dialog for automatic accessor creation

0
Dmitri Zagidulin On

@Rekha - In case you find this useful in your learning, take a look at the Updated Pharo By Example book. (Specifically, the 'Pharo Object Model' chapter, which is exactly about the topic of this question -- instance creation, class variables, inheritance, etc). The book is a work in progress (we're updating the old 'Pharo by Example' book to refer to the new version of Pharo), but should still prove useful.