Why #method? WAS "Re: [Newbies] Multiple constructors"
nicolas cellier
ncellier at ifrance.com
Fri May 12 22:21:01 UTC 2006
Le Vendredi 12 Mai 2006 23:22, Bob Erb a écrit :
> Here's a question I feel comfortable asking on the Newbies list. :-)
>
> What's the hash mark signify when used as a prefix to a method
> name, as in the snippet below?
>
> On 5/12/06, Ralph Johnson <johnson at cs.uiuc.edu> wrote:
> > Because instance creation methods are just methods (but on the class,
> > instead of on the instance), you can define as many as you want. Look
> > at class Date. There is #today, #tomorrow, #yesterday, #newDay:year:,
> > #newDay:month:year:, #year:day: #year:month:day:
>
> - Bob Erb
> _______________________________________________
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
Well, #today is a literal constant in Smalltalk syntax.
It creates an instance of Symbol.
More exactly, it return existing Symbol made of letters #($t $o $d $a $y)
because Symbol are some special String that are unique in the image (a kind
of optimization of space with efficient equality testing).
When you send a message to an object, the selector of the message is a Symbol.
This Symbol will be used as a look up key in the methodDictionary of
receiver's class, so as to find wich method to execute in response to the
message send.
So when we write Time class>>today, that means the method with selector #today
in class (Time class).
Time class>>today is also the way to print method stack in the debugger.
So it is widely used in our mail to designate precisely that method.
But it is not re-interpretable as a Smalltalk expression.
This is why we sometimes write Time class>>#today instead, an expression which
you should try and inspect.
Lazily, we omit the class side when there are several methods, but we indicate
the # to still make clear we are implicitly speaking of a method selector.
Note that (Time class) means that it is a class method, thus you must send the
message to the class, not to the instances.
Example:
Time today
If the method is an instance method, like #monthName, you would write
Time>>monthName or Time>>#monthName.
Example:
(Time today) monthName
Hope this explanation clarify our habits
Nicolas
More information about the Beginners
mailing list