Routines and fork

Hi

Why is it that when .fork is used a routine can be played again without reset?
What am I missing here?

h = ({ 2.wait;  "hey I'm here".postln })
h.fork; //works
h.fork; //and again...

o = Routine({ 2.wait; "hey it's me".postln })
o.play; //yup
o.play; //um no 
o.reset;
o.play; //and again

Daniel

Let me demonstrate a different way.

a = [1];

b = a.asArray.add(2);
c = a.asArray.add(3);

I think you’ll get [1, 2, 3] here because asArray doesn’t create a new array from an array.

But:

a = 1;

b = a.asArray.add(2);
c = a.asArray.add(3);

Now b will be [1, 2] and c will be [1, 3] because asArray, when called on something that isn’t an array, returns a new array. So b and c end up being separate new arrays.

A Routine is just its own routine (and in fact, Routines can’t be copied). But fork creates a new routine from a function, and it doesn’t have access to any other routines that had been created previously. So they must be separate routines with independent states.

hjh

1 Like