[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [sc-users] Pseudo-method question



One thing to consider is what gets returned from the function when you run it. If you are going to have functions return a value from the prototype object, you will want the function to return the last line. Using double curly braces will prevent this from happening.
Please consider the examples below:

///set with dot syntax single curly pair, call with dot syntax
m = ();
m.dlt = {arg self ...data; "TEST: Self: % Args: %".format(self, data).postln; 999;};
m.dlt([11,22,33]);// performs function, returns 999

///set with dot syntax double curly pair, call with dot syntax
m = ();
m.dlt = {{arg self ...data; "TEST: Self: % Args: %".format(self, data).postln; 999;}};
a = m.dlt([11,22,33]);// does not perform function, returns the inner function
a.def.sourceCode;

///set with dot syntax double curly pair, call with dot syntax and then .value
m = ();
m.dlt = {{arg self ...data; "TEST: Self: % Args: %".format(self, data).postln; 999;}};
a = m.dlt.value([11,22,33]);// performs function but args are not as expected, returns 999

///set with dot syntax double curly pair, call with dot syntax  and then .value using the Event as first arg
m = ();
m.dlt = {{arg self ...data; "TEST: Self: % Args: %".format(self, data).postln; 999;}};
a = m.dlt.value(m, [11,22,33]);// performs function and arg are in defined order, returns 999

I would argue that the last two examples are indeed hacks. You always need to have the Event itself as the first arg, which is arguably redundant.

Either way you choose, try to stick to one way consistently. My preference is the first example.
If you are careful to not use any function names that the object already responds to, you should be fine. To make sure:
m.respondsTo('myNewFunctionName');// -> false
m.respondsTo('play');// -> true

If you do override method names you should be careful that some methods will override and some won't:
s.boot;
().play; //plays note
(play: {|self| "PLAYING - Self: %".format(self).postln;}).play; // overrides 'play', posts message but doesn't play note
(put: {|self, key, val| "PUTTING: Self: % Key: % Val; %".format(self, key, val).postln;}).put(\hallo, 88); //does not override 'put'


+Eirik