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.
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:In my example
name:email:phone:
is the method's selector anduserName
,userEmail
anduserPhone
are parameters. You can call this method as in your example.name
andemail
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:
and a class side method:
or a shorter version using cascaded messages:
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:
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