Why does this work? Assign after use in MIDIdef

var sli;
   arg vel;

sli=Slider().action_({ ...});


sli is assigned after ~midi is defined, but it is still reachable by ~midi. How does this happen?

OK… take a look at this block of code. What do you think it will print?

var func = {
	"we are now inside the function".postln;
	(x + y).postln;

var x, y;

x = 3;
y = 5;

"x and y have been assigned values".postln;

"now calling the function".postln;

You actually get this:

x and y have been assigned values
now calling the function
we are now inside the function
-> 8

You might notice that the body of the function has been written at the beginning, but nothing inside the braces evaluates immediately.

The x and y variables are accessed inside the braces, but this does not happen until func.value at the end.

In your example, the MIDIdef function does not evaluate until a MIDI message comes in. So, at the moment of creation, it doesn’t matter what sli is because the code using sli is not running at that time… it only matters that it’s been assigned before a MIDI message is processed.


1 Like

Normal behaviour with the functions makes sense.

For some reason I had the impression that for certain cases, as in with MIDIdef.cc, variables get resolved first.