| 1 |
// test basic static initialization #2024-static-init |
| 2 |
|
| 3 |
//----------------------------------------- |
| 4 |
// STATIC VARAIBLE INITIALIZATION SEMANTICS |
| 5 |
//----------------------------------------- |
| 6 |
// static variables can only be delcared: |
| 7 |
// a) within a class definition |
| 8 |
// b) outside of any class function defintion |
| 9 |
// c) at the outer-most class scope (cannot be in nested { }) |
| 10 |
//----------------------------------------- |
| 11 |
// COMPILE-TIME checks: |
| 12 |
// 1) a statement containing a static variable declaration |
| 13 |
// CANNOT access member data/functions |
| 14 |
// 2) otherwise, a statement can access both static and member data. |
| 15 |
// 3) a statement containing a static variable declaration |
| 16 |
// CANNOT access local (i.e., outside of class def) vars or funcs |
| 17 |
// even in non-public classes |
| 18 |
//----------------------------------------- |
| 19 |
// RUNTIME checks: |
| 20 |
// 4) static initialization statements are run in immediate mode |
| 21 |
// meaning no time advances; this is enforced at runtime; any |
| 22 |
// time advances, including waiting on events, will result in |
| 23 |
// a runtime exception |
| 24 |
//----------------------------------------- |
| 25 |
|
| 26 |
// a class |
| 27 |
public class Foo |
| 28 |
{ |
| 29 |
// int |
| 30 |
1 => static int S_INT; |
| 31 |
// float |
| 32 |
2 => static float S_FLOAT; |
| 33 |
// dur |
| 34 |
3::second => static dur S_DUR; |
| 35 |
// time |
| 36 |
now + 4::second => static time S_TIME; |
| 37 |
// vec3 |
| 38 |
@(5,6,7) => static vec3 S_VEC3; |
| 39 |
// array |
| 40 |
[8,9,10,11] @=> static int S_INT_ARRAY[]; |
| 41 |
// string |
| 42 |
static string S_STRING("12"); |
| 43 |
// ugen |
| 44 |
static SinOsc S_SINOSC(440); |
| 45 |
} |
| 46 |
|
| 47 |
// print static variables |
| 48 |
<<< Foo.S_INT >>>; |
| 49 |
<<< Foo.S_FLOAT >>>; |
| 50 |
<<< Foo.S_DUR / second >>>; |
| 51 |
<<< (Foo.S_TIME-now) / second >>>; |
| 52 |
<<< Foo.S_VEC3 >>>; |
| 53 |
for( auto i : Foo.S_INT_ARRAY ) <<< i >>>; |
| 54 |
<<< Foo.S_STRING >>>; |
| 55 |
<<< Foo.S_SINOSC.freq() >>>; |