References for primitives.

From CSWiki
Jump to: navigation, search

I'd like to be able to create references to primitives (ints, floats, etc) in Chuck. Why? To be able to write a function that is sporked that modifies a variable while I use it somewhere else. Example:

 int yourmom;                                                                    
 fun void count(int@ value) {                                                    
     while (true) {                                                              
         value+1 => value;                                                       
         500::ms => now;                                                         
     }                                                                           
 }                                                                               
 spork ~ count(yourmom);                                                         
 while (true) {                                                                  
     1::second => now;                                                           
     <<<"yourmom",yourmom>>>;                                                    
 }

More realistic reasons though are for creating classes that wait on certain types of events and then modify some given variables. Like if I have a function modulating a sine wave, but I want the volume of it to be modified from an OSC message, I'd like to be able to write a general-purpose function that waits for an OSC event and modifies a float:

 fun void OscFloat(float@ value, string oscpath)
 {
    .. wait for OSC event for "oscpath"+",f" ..
    .. OSC_value .. => value;
 }
 fun void modulate()
 {
    float volume;
    spork ~ OscFloat(volume, "/oscillator/volume");
    while (true) {
       10::ms => now;
       lfo1.last()*500 => oscillator.freq;
       volume => oscillator.gain;
    }
 }
 spork ~ modulate();

As things stand currently, the above code gives the "cannot declare references of primitive type" error. This means that you basically have to use global variables and write a different OSC function for every variable.