Hi, all
Beginner here trying to get the hang of the syntax.
Using the ~array.at(n) inside a .do - I get an error message. But with the shortcut ~array[n] - is works.
I thought these ways of writing was the same? Or is it more than a shortcut?
In “x = 1” the left hand side is an identifier, and the equals sign means assignment.
In “o.x = 1” the left hand side is a unary message send, and the equals sign is a syntax for renaming the message selector and appending an argument.
It’s a convention in Sc for “getter” and “setter” selector pairs that the “setter” selector has the name of the “getter” with an underscore appended.
(There’s a mechanism to automatically generate such getter and setter methods for class and instance variables.)
This is worth knowing because Sc error messages assume you do, i.e.
o = 0@0; o.z = 1 // => ERROR: Message ‘z_’ not understood.
Also because setter calls chain nicely.
o = 0@0; o.x_(1).y_(1).dist(0@0) == 2.sqrt
Ps. Drifiting off topic, there are other ways of making C-like notations for Smalltalk-like languages that allow the simpler setter notation of “o.x(1)”, but they don’t interact well with allowing messages to have both too few and too many arguments.