Reputation: 301
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?.
Upvotes: 5
Views: 559
Reputation: 1259
The return symbol, ^, is one of the few language built-ins constructs. Smalltalk will return the value of the expression following the ^ symbol.
Upvotes: 3
Reputation: 4623
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.
Upvotes: 10
Reputation: 1594
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
Upvotes: 1