Debian Codegraph @

examples/class/static-init.ck

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