Class Methods

Damien Cassou, Stéphane Ducasse and Luc Fabresse http://stephane.ducasse.free.fr

Class Methods

  1. in Pharo, everything is an object
  2. objects can receive messages
  3. classes are objects too

Classes can receive messages

Examples

Time now
> 9:18:36.304688 pm

The message now is sent to the class Time

Date today
> 29 July 2015

The message today is sent to the class Date

Examples

FileLocator workingDirectory
ZnEasy getPng: 'http://pharo.org/web/files/pharo.png'
ZnServer startDefaultOn: 8080

Class Methods are Defined on Class Side

Note the Class button pressed!

Common Mistake

Counter class >> withValue: anInteger
   self new
      value: anInteger;
      yourself

Counter withValue: 10 returns the class Counter instead of a new instance

Why?

Counter class >> withValue: anInteger
   self new
      value: anInteger;
      yourself

is equivalent to

Counter class >> withValue: anInteger
   self new
      value: anInteger;
      yourself.
   ^ self

self here is the class Counter (the receiver of the message)

Solution

Counter class >> withValue: anInteger
   ^ self new
      value: anInteger;
      yourself

Summary

/