2 summary:: Method calls, sending messages to objects
3 categories:: Language>OOP
4 related:: Reference/Classes, Classes/Class, Classes/Object, Guides/Intro-to-Objects, Guides/Polymorphism
8 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.
9 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.
11 Messages may be written using binary operators, functional notation or receiver notation.
13 section:: Binary operator messages
15 A binary operator selector is any string of characters from the list of legal binary operator characters:
17 ! @ % & * - + = | < > ? /
20 An exception is that no operator may begin with code:://:: or code::/*:: which are comment delimiters.
22 A binary operator expression consists of two expressions with a binary operator between them.
24 1 + 2 // sum of one and two
25 a - b // difference of a and b
26 x < 0.0 // answer whether x is less than zero
29 A binary operator can also be an identifier followed by a colon.
34 section:: Operator Precedence
36 There is none. All binary operators have the same level of precedence and associate from left to right.
37 For example, the expression:
49 Therefore it is usually better style to fully parenthesize your expressions.
51 section:: Functional notation messages
53 The message selector preceeds the parenthesized argument list. The first argument in the list is actually
57 max(a, b) // maximum of a and b
60 section:: Receiver notation messages
62 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.
96 section:: Default Argument Values
98 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.
99 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 link::Reference/Functions::).
100 When calling a method or function with zero arguments you can omit the parentheses:
102 // x is declared to take two arguments a and b which default to 1 and 2 respectively.
103 // It returns their sum. This syntax will be explained in the section on Functions.
104 x = { arg a=1, b=2; a + b };
106 z = x.value; // z is set to 3. (a defaults to 1, b defaults to 2)
107 z = x.value(10); // z is set to 12. (a is 10, b defaults to 2)
108 z = x.value(10, 5); // z is set to 15. (a is 10, b is 5)
109 z = x.value(10, 5, 9); // z is set to 15. (a is 10, b is 5, 9 is ignored)
112 section:: Keyword Arguments
114 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.
115 Any argument may be passed as a keyword argument except for the receiver code::this::.
116 Keyword arguments must come after any normal (aka emphasis::positional::) arguments, and may be specified in any order.
117 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:
119 keywordWarnings(false)
121 If a keyword argument and a positional argument specify the same argument, then the keyword argument value overrides the positional argument value.
123 For example the code::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.
125 SinOsc.ar(800, pi, 0.2, 0); // all normal arguments: freq, phase, mul, add
127 // freq = 800, mul = 0.2, others get default values.
128 SinOsc.ar(800, mul: 0.2);
130 // freq = 800, phase = pi, mul = 0.2, add gets its default value of zero.
131 SinOsc.ar(phase: pi, mul: 0.2, freq: 800);
133 // keyword value of 1200 overrides the positional arg value of 800
134 SinOsc.ar(800, freq: 1200);
137 SinOsc.ar(zorg: 999); // invalid keyword prints warning
139 The arguments to a Function may also be specified by keyword arguments when using the 'value' message.
141 // function args may be specified by keyword.
142 { arg a=1, b=2, c=3; [a, b, c].postln }.value(b: 7, c: 8);
144 You may also use keyword arguments when using the 'perform' method.
146 SinOsc.perform('ar', phase: pi, mul: 0.2, freq: 800);
148 subsection:: Cost of using keyword arguments
150 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.