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)"
?
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)"
?
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.
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
© 2022 - 2024 — McMap. All rights reserved.