What is this class definition syntax? Another surprising feature in sclang

After the surprising discovery of constants, I stumbled on another surprising syntax recently. I kind of understand what it does, but can someone explain it’s usage and what to call it (for the tree-sitter SuperCollider parser) ?

The bit I mean specifically is the square brackets with a word in them, eg [char].

String[char] : RawArray {}

Some examples from the class library:

aha, this is an indexed type where the identifier in the square brackets can be one of slot, double, float, int8, int16, int32, char.

Yes, the bracket construction enables the class to specify the type of it’s slots. One thing I didn’t know: these actually determine how memory is allocated for this object - e.g. a Class[float] will only allocate sizeof(float) for it’s instance variables. One thing that’s curious to me - it seems implicit that ALL classes are [slot] unless otherwise specified. But maybe there are some subtleties I haven’t found in the code yet…

1 Like

One thing that’s curious to me - it seems implicit that ALL classes are [slot] unless otherwise specified.

It’s possible Sc does this differently, but in Smalltalk only some classes have indexed instance variables:

Indexed Instance Variables: Instances of some classes can have instance variables that are not accessed by names. These are called indexed instance variables. Instead of being referred to by name, indexed instance variables are referred to by messages that include integers, called indices, as arguments. Since indexing is a form of association, the two fundamental indexing messages have the same selectors as the association messages to dictionaries–at: and at:put:. […] A class whose instances have indexed instance variables can also have named instance variables. All instances of such a class will have the same number of named instance variables, but may have different numbers of indexed variables. (Blue Book, p. 46)

Classes with indexed variables have a “new:” constructor instead of “new”, the argument sets the number of indexable variables, and also a “size” message that gives the number of indexed variables, c.f.

A similar pair for creating variable length objects, new: and basicNew:, are also provided in the protocol of class Behavior. (Note, this technique of dual messages is also used in class Object for accessing messages such as at: and at:put:.) (p. 274)

The Squeak comments for these are quite nice, copied below.

basicNew: sizeRequested
“Primitive. Answer an instance of this class with the number of indexable
variables specified by the argument, sizeRequested. Fail if this class is not
indexable or if the argument is not a positive Integer, or if there is not
enough memory available. Essential.”

basicAt: index
“Primitive. Assumes receiver is indexable. Answer the value of an
indexable element in the receiver. Fail if the argument index is not an
Integer or is out of bounds. Essential.”

basicAt: index put: value
“Primitive. Assumes receiver is indexable. Store the second argument
value in the indexable element of the receiver indicated by index. Fail
if the index is not an Integer or is out of bounds. Or fail if the value is
not of the right type for this kind of collection. Answer the value that
was stored. Essential.”

1 Like