Separate with semicolons, yes.
There are two other points to watch out for:
Variable declarations
Declaring variables in any of these ( )
blocks breaks compilation if you have multiple blocks. I had thought it would be OK if variables were declared in the first block only, but this is not the case.
(
var a = 1, b = 2;
(a + b).postln;
);
(
5.postln;
);
ERROR: syntax error, unexpected ';', expecting $end
in interpreted text
line 4 char 2:
);
A nice workaround is to wrap any blocks that declare variables within a function, and evaluate the function. Below, you can run all of it at one time, or you could select one or the other block and run them independently.
(
{
var a = 1, b = 2;
(a + b).postln;
}.value; // <<-- don't forget '.value' here!
);
(
5.postln;
);
// prints:
3
5
Asynchronous operations
(
b = Buffer.read(s, Platform.resourceDir +/+ "sounds/a11wlk01.wav");
);
(
b.numFrames.debug("numFrames");
)
// prints:
nil // :-O
In an example like this, numFrames won’t be ready right away – you need to wait for it.
If you only want to run the entire file, then it’s pretty easy: fork a routine and s.sync
in the middle. The following works – but you can’t run the statements separately anymore. (If you’re loading multiple buffers, you don’t need to .sync
each one – you can do several buffers and then s.sync
and it will wait for all of the pending commands to finish.)
(
// 'waitForBoot' automatically forks the routine for you
s.waitForBoot {
b = Buffer.read(s, Platform.resourceDir +/+ "sounds/a11wlk01.wav");
s.sync;
b.numFrames.postln;
}
)
// prints:
188893
If you need to run the blocks separately sometimes as well, then it gets tricky. Here’s one way – you can actually run the two ( )
blocks in order or reversed! But this is probably not worth the effort if your main purpose is to run everything all at once.
c = CondVar.new;
(
b = Buffer.read(s, Platform.resourceDir +/+ "sounds/a11wlk01.wav", action: { c.signalAll });
);
(
fork {
c.wait { b.numFrames.notNil };
b.numFrames.postln;
};
)
hjh