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 may be specified by the name by which they are declared in a method's definition. Such arguments are called keyword arguments. Any argument may be passed as a keyword argument except for the receiver this
. Keyword arguments must come after any normal (aka positional) arguments, and may be specified in any order. If a keyword is specified and there is no matching argument then it is ignored and a warning will be printed. This warning may be turned off globally by making the following call:
If a keyword argument and a positional argument specify the same argument, then the keyword argument value overrides the positional argument value.
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 may also be specified by keyword arguments 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.