Array method shortcuts - at - beginner

Hi, all
Beginner here trying to get the hang of the syntax.

Using the 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?

x = [nil,nil,nil];{ arg n; = rrand(1,10);	

x = [nil,nil,nil];{ arg n;
	x[n] = rrand(1,10);	

x[n] = is “put,” not “at.”



Thanks. I see, so:

x[n] = // put shortcut
x[n]  // at shortcut

Exactly. Also, different but related, p.q = 1 is syntax for p.q_(1).

o = 0@0; (o.x = 1) == (o.x_(1))

Thanks for chiming in @rdd Actually I’ve been wondering exactly about what you write, though I can’t wrap my head around it - even after your example. :sweat_smile:

To be, sometimes in the language ‘_()’ is the same as ‘=’. E.g:

t =;
t = = true; // both works

a = "foo";
a_("foo"); // only first work.

What determines when these are interchangeable? I guess the answer is in your example, but are there a different way of explaining it?

Many thx,

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.

o = 0@0; [o.x == 0, o.x() == 0, o.x(nil) == 0, o.x_.x == nil, o.x_().x == nil, o.x_(0, nil).x == 0]

1 Like

Thanks for explaining that @rdd