Sending messages is the way things get done in an object oriented language. A message consists of a message selector which names the type of operation, a receiver to which the message is sent and in some cases a list of arguments which give additional information pertaining to the operation. A message always returns a result. The kind of result depends on the kind of message. In the default case, the return value is the receiver itself.
Messages may be written using binary operators, functional notation or receiver notation.
A binary operator selector is any string of characters from the list of legal binary operator characters:
An exception is that no operator may begin with // or /* which are comment delimiters.
A binary operator expression consists of two expressions with a binary operator between them.
A binary operator can also be an identifier followed by a colon.
There is none. All binary operators have the same level of precedence and associate from left to right. For example, the expression:
is equivalent to:
and not:
Therefore it is usually better style to fully parenthesize your expressions.
The message selector precedes the parenthesized argument list. The first argument in the list is actually the receiver.
A method call in functional notation may be converted to receiver notation by putting the receiver before the method name followed by a dot as shown below.
another example:
You may call a function or method with more or fewer arguments than it was declared to accept. If fewer arguments are passed, those arguments not passed are set to a default value if one is given in the method or function definition, or otherwise to nil. If too many arguments are passed, the excess arguments are either collected into an Array or ignored depending on whether or not the function or method has an ellipsis argument (explained in Functions). When calling a method or function with zero arguments you can omit the parentheses:
Arguments to methods and functions may be specified either by position or by name. Arguments specified by position are called positional arguments, and are interpreted according to the order in which they are declared in the method or function definition. Arguments specified by name (followed by a colon) are called keyword arguments. Any argument may be passed as a keyword argument except for the receiver this.
Keyword arguments must come after any positional arguments, may be specified in any order among themselves, and once a keyword argument is used, no positional argument may follow. In other words, if you skip any argument defined before the one you want to specify, you must use keyword arguments. So once you use a keyword argument, all subsequent arguments must also be keyword arguments.
If a keyword is specified and there is no matching argument, it is ignored and a warning will be printed. This warning may be turned off globally by making the following call:
If the same argument is specified more than once, whether by positional or keyword arguments, the rightmost value overrides all previous ones. Note that this usage, while legal, is discouraged as it can be confusing.
For example, the ar class method of the SinOsc class takes arguments named freq, phase, mul, and add in that order. All of the following are legal calls to that method.
The arguments to a Function instance work the same way when using the 'value' message.
You may also use keyword arguments when using the 'perform' method.
When using keyword arguments there is a runtime cost to do the matching that you should be aware of. The cost can be worth the convenience when speed is not critical.