Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smalltalk Return Precedence

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?.

like image 251
Jatajuuf Avatar asked Jun 19 '11 10:06

Jatajuuf


Video Answer


3 Answers

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.

like image 86
Vanessa Freudenberg Avatar answered Oct 04 '22 09:10

Vanessa Freudenberg


The return symbol, ^, is one of the few language built-ins constructs. Smalltalk will return the value of the expression following the ^ symbol.

like image 22
SHODAN Avatar answered Oct 04 '22 11:10

SHODAN


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

like image 32
Nico Avatar answered Oct 04 '22 10:10

Nico