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

2

5

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)"?

Hohenlinden answered 23/4, 2020 at 17:23 Comment(1)
@ is not an operator, it’s a message just like almost everything. The basis of Smalltalk is messages sent to objects.Driblet
D
6

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.

Dogfish answered 23/4, 2020 at 18:4 Comment(0)
R
1

In Pharo it is a method defined in the Number class

@ y 
"Primitive. Answer a Point whose x value is the receiver and whose y 
value is the argument. Optional. No Lookup. See Object documentation 
whatIsAPrimitive."

<primitive: 18>
^Point x: self y: y
Renowned answered 27/4, 2020 at 13:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.