Search code examples
pharo

What is the at ("@") operator in Pharo?


I've looked online for a meaning of the @ operator in Pharo, but couldn't find anything.

What's the meaning of the Pharo @ operator? For instance, why does 25@50 get evaluated as: "(25@50)"?


Solution

  • In Smalltalk, the @ symbol is used to create instances of the class Point. An instance of such a class has two ivars x and y. You can create a Point using the x:y: message, like this

      Point x: 3 y: 4.
    

    However, it is less verbose to use the message @ like this

      3 @ 4
    

    to create the same thing.

    Note that while x:y: is a message you send to the class Point, the message @ 4 is sent to the integer 3. In other words, the former is a class message, the latter an instance message.

    Note that, since many people write 3@4 instead of 3 @ 4, this has the risk of creating a surprising side effect. In fact

      3@-4
    

    should be (in principle) the Point with coordinates 3 and -4. However, the Smalltalk syntax is different and will parse it as the message with selector @- and argument 4 sent to the receiver 3. This is why some dialects make an exception so that the message is interpreted as 3 @ -4, which can be achieved by implementing the method @- in Number or by tweaking the parser.