Search code examples
user-interfacesmalltalkpharosqueakmorphic

Meaning of @-sign in GUI definition of a shape?


For instance, in the Lessphic Tutorial (page 6), the following is written:

aShape := ShapedView withShape: (0@0 corner: 100@100).
aShape := (0@0 corner: 100@100) shapedView.

What does “@” mean? Is it a point with x and y coordinates? This notation perhaps originated in morphic. But “@-sign” is difficult to google.


Solution

  • Even though one might be confused by the use of symbols like @, +, =, etc., and think of them as reserved (i.e., known to the compiler), in Smalltalk all of them are selectors of regular methods. In other words, they are not reserved tokens but valid selectors of messages that (in most of the cases) implement the expected behavior.

    In the particular case of @, if we search for implementors we will find one in Number, usually implemented as

    @ aNumber
      ^Point x: self y: aNumber
    

    (In Pharo, however, there is a primitive for speeding things up, which is not needed otherwise)

    The reason for this message is to provide a less verbose Point creation method, so that the client only has to write, say 200 @ 300, instead of Point x: 200 y: 300.

    Note by the way that this pattern is pretty common in Smalltalk. Some few questions ago in this [smalltalk] tag we discussed the message / which is a shortcut of the same sort: 2 / 3 is equivalent to (Fraction numerator: 2 denominator: 3) reduced. Other examples include 'hello world' readStream for ReadStream on: 'hello world', '234.5' asNumber for Number readFromString: '234.5', etc., etc.