Objects, Classes, Messages, Arguments

What is a message, anyway? Glad you asked.

SuperCollider is an Object-Oriented programming language, like Java or C++. It is beyond the scope of this tutorial to explain what this means, so we’ll let you search that on the web if you are curious. Here we’ll just explain a few basic concepts you need to know to better understand this new language you are learning.

Everything in SuperCollider is an object. Even simple numbers are objects in SC. Different objects behave in different ways and hold different kinds of information. You can request some info or action from an object is by sending it a message. When you write something like 2.squared, the message squared is being sent to the receiver object 2. The dot between them makes the connection. Messages are also called methods, by the way.

Objects are specified hierarchically in classes. SuperCollider comes with a huge collection of pre-defined objects, each with their own set of methods.

Here’s a good way to understand this. Let’s imagine there is an abstract class of objects called Pet. The Pet class defines a few general methods (messages) common to all pets. Methods like picture, name, address, age could be used to get information about the pet. Methods like run, eat, sleep would make the pet perform a specific action. Then we could have two subclasses of Pet: one called Dog, another called Cat. The subclasses of Pet inherit all the methods from their parent class. For example, both Dog.name and Cat.name would return the name of the pet. Both would also happily respond in a similar way to the .eat message. Those are actions common to all objects of class Pet). At the same time, subclasses often implement new methods of their own, adding specialized features. The Dog class will likely have a bark method, so you can call Dog.bark and it will know what to do. Cat.bark would throw you an error message: ERROR: Message ’bark’ not understood.

In all these hypothetical examples, the words beginning with a capital letter are classes which represent objects. The lowercase words after the dot are messages (or methods) being sent to those objects. Sending a message to an object always returns some kind of information. Finally, messages sometimes accept (or even require) arguments. Arguments are the things that come inside parentheses right after a message. In Cat.eat(sardines, 2), the message eat is being sent to Cat with some very specific information: what to eat, and quantity. Sometimes you will see arguments declared explicitly inside the parentheses (keywords ending with a colon). This is often handy to remind the reader what the argument refers to. Dog.bark(volume: 10) is more self-explanatory than just Dog.bark(10).

OK—enough of this quick and dirty explanation of object-oriented programming. Let’s try some examples that you can actually run in SuperCollider. Run one line after the other and see if you can identify the message, the receiver object, and the arguments (if any). Answers at the end of this document.

.reverse;
"hello".dup(4); 
3.1415.round(0.1); // note that the first dot is the decimal case of 3.1415
100.rand; // evaluate this line several times
// Chaining messages is fun:
100.0.rand.round(0.01).dup(4);