I'd like a little clarification on the precedence of the return statement, which appears to go against the general precedence rules.
For example the expression
^ 2 + 3.
returns 5(which is what I want) but shouldn't it return just 2 since Unary operators of which ^ is one has higher precedence over + which is Binary?.
There are no "unary operators" in Smalltalk. There are only 3 precedence levels: unary messages ("receiver message"), binary operators ("receiver + argument"), and n-ary keyword messages ("receiver message: argument1"). In all cases the receiver comes first.
So "^" is not an operator, but indicates a return statement. Similarly, in "-4" the "-" is not an operator but part of the number literal.
The return symbol, ^, is one of the few language built-ins constructs. Smalltalk will return the value of the expression following the ^ symbol.
Is Smalltalk's ^ really an operator at all ? I guess it is rather a reserved symbol. And what should happen to the "dangling" + 3 then, when the surrounding method has returned? I think the behavior is correct as the return statement is the last statement to happen in a "normal" Smalltalk method.
Regards
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With