2 summary:: the ability of different classes to respond to a message in different ways
3 categories:: Language>OOP
4 related:: Reference/Messages, Reference/Classes, Guides/Intro-to-Objects
8 Polymorphism is the ability of different classes to respond to a message in different ways. A message generally has some underlying meaning and it is the responsibility of each class to respond in a way appropriate to that meaning.
10 For example, the code::value:: message means "give me the effective value of this object".
12 The value method is implemented by these classes (among others):
14 ## Function : || this.value(args)
15 ## Object : || this.value()
16 ## Ref : || this.value
19 Let's look at how these classes implement the value message.
23 Here's the value method in class link::Classes/Object:: :
27 It simply returns itself. Since all classes inherit from class Object this means that unless a class overrides code::value::, the object will respond to code::value:: by returning itself.
29 5.postln; // posts itself
30 5.value.postln; // value returns itself
32 'a symbol'.value.postln;
38 In class link::Classes/Function:: the value method is a primitive:
42 // evaluate a function with args
47 code::_FunctionValue:: is a C code primitive, so it is not possible to know just by looking at it what it does. However what it does is to evaluate the function and return the result.
49 { 5.squared }.postln; // posts Instance of Function
50 { 5.squared }.value.postln; // posts 25
54 The link::Classes/Ref:: class provides a way to create an indirect reference to an object. It can be used to pass a value by reference. Ref objects have a single instance variable called code::value::.
55 The code::value:: method returns the value of the instance variable code::value::. Here is the class definition for Ref:
57 Ref : AbstractFunction
60 *new { arg thing; ^super.new.value_(thing) }
61 set { arg thing; value = thing }
63 dereference { ^value }
66 //behave like a stream
68 embedInStream { arg inval;
69 ^this.value.embedInStream(inval)
73 stream << "`(" << value << ")";
76 stream << "`(" <<< value << ")";
80 Here is how it responds :
83 Ref.new(123).value.postln;
86 Ref also implements a message called code::dereference:: which is another good example of polymorphism. As implemented in Ref, dereference just returns the value instance variable which is no different than what the value method does.
87 So what is the need for it? That is explained by how other classes respond to dereference. The dereference message means "remove any Ref that contains you".
88 In class Object dereference returns the object itself, again just like the value message. The difference is that no other classes override this method. So that dereference of a Function is still the Function.
90 ## Object : || this.dereference()
91 ## Ref : || this.dereference()
95 { 5.squared }.value.postln;
96 Ref.new(123).value.postln;
99 { 5.squared }.dereference.postln;
100 Ref.new(123).dereference.postln;
104 Yet another example of polymorphism is play. Many different kinds of objects know how to play themselves.
106 subsection:: Function
108 { PinkNoise.ar(0.1) }.play;
111 subsection:: AppClock
115 w = Window.new("trem", Rect(512, 256, 360, 130));
117 r = Routine({ arg appClockTime;
118 ["AppClock has been playing for secs:",appClockTime].postln;
121 w.bounds = w.bounds.moveBy(10.rand2, 10.rand2);
122 w.alpha = cos(i*0.1pi)*0.5+0.5;
131 subsection:: SynthDef
134 x = SynthDef("Help-SynthDef", { arg out=0;
135 Out.ar(out, PinkNoise.ar(0.1))
142 Pbind(\degree, Pseq([0, 1, 2, 3],inf)).play;
146 Polymorphism allows you to write code that does not assume anything about the implementation of an object, but rather asks the object to "do what I mean" and have the object respond appropriately.