Syntax of Class and Instance Variables and Methods in Pharo 4.0

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

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

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

      

To call this static method of CreateUser class 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 the same as above, but it will be on the side of the class instance. However, when I call the method, I will call it with the "new" keyword to create a new instance as shown below:

CreateUser new
     name:userName
     email:userEmail
     phone:userPhone

      

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

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

      

However, when I go to the CreateUser class for re-validation, I see that the above method is created on the class side as:

CreateUser:name:email:phone:

      

My queries are as follows: 1. What am I doing wrong above? How can I fix the above error? 2. Is the concept of using static variables / methods versus class variables / methods the same as Java? 3. If I want to access the above instance variables, I can add accessor methods for the class / instance and then call them with an instance of the class / class instance object. It is right?

Any help you can give would be greatly appreciated! Thank you very much in advance.

+3


source to share


2 answers


I am assuming that you are misunderstanding the syntax of the method because it createNewUser:Arguments

does not make sense. What you need is a class-side method 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:

, this is a method selector and userName

, userEmail

and userPhone

are parameters. You can call this method as in your example. name

and email

refer to either the class side on the instance side, depending on where this method is defined.

Nor should the class be named CreateUser

. Think about it, what calls will be called? "createUsers"? Usually you call the class User, then you can think of instances as "users" and then the responsibility for the class object is "create users (instances of them)".

Note that it is weird to have such a method on the class side. Usually you create an instance method:

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

      

and a class method:

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

      



or a shorter version using cascading 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 little simpler. Since everything is an object, the class is also an object, so class variables and class methods are instance variables and methods of the class, which is an instance of the "class class". The following photo can help you understand the associations between objects in Pharo:

enter image description here

It can be a little confusing in the beginning, but in the end you don't have static / non-static methods / variables either, you only have objects, instances and inheritance.

So what you have to think about, what are you going to set objects about. You should probably ask the User about this via email or mobile number, but you will ask the Custom Class to create a user or find a user or suggest a default t-shirt size for the user.

3) Yes, you must make an accessory. Also, if you select a class in the system browser and press Cmd + H + A (or Ctrl or Alt) depending on your OS, you will get a dialog to automatically create access

+9


source


@Rekha - If you find it helpful in your tutorial take a look at Updating Pharo By Example . (In particular, the chapter "Phage Object Model", which deals exactly with the topic of this question: instantiation, class variables, inheritance, etc.). The book is a work in progress (we are updating the old book "Pharo by Example" to switch to the new version of Pharo), but it will still be useful.



0


source







All Articles