Confused by .play and .set

Hi all,

If I assign a variable and use .set to change it’s arguments, the following works fine:

(
	x = {
	arg freq = 400, amp = 0.5;
	SinOsc.ar(freq, 0,amp);
	}.play;
)

x.set(\freq, 700);

It seems however that I can’t first assign x, then assign play, and then use set? For example the following doesn’t work the same way (executed consecutively) :


(
	x = {
	arg freq = 400, amp = 0.5;
	SinOsc.ar(freq, 0,amp);
	};
)

x.play;

x.set(\freq, 700);

Why should it be any different?

If you wrote this:

x = 1 + 2;
(x * 3).postln;

y = 1;
y + 2;
(y * 3).postln;

… do you expect the x and y lines to post the same things?

It’s the same as in your example. You start with a function, and play changes it into a synth (just like you can start with 1, and + 2 changes it into a different number). In the first case, it’s the new thing that gets saved into x. In the second, it’s only the original thing that gets put into the variable, and the changed thing is thrown away.

It’s the result of play that you need in the variable (just as in the math example, it’s the result of + 2 that you need to assign).

hjh