Debian Codegraph @

src/test/01-Basic/256-type-type.ck

raw
1 //---------------------------------------------------------------------
2 // name: type-type.ck
3 // desc: this example shows working with the Type type in ChucK;
4 // a Type instance represents a specific type currently in
5 // in the ChucK VM
6 //
7 // version: needs 1.5.0.0 or higher
8 //
9 // author: Ge Wang (https://ccrma.stanford.edu/~ge/)
10 // date: Winter 2023
11 //---------------------------------------------------------------------
12
13 // print some info about a Type
14 fun void about( Type t )
15 {
16 // divider
17 cherr <= "--------------------------------------------------" <= IO.newline();
18 // check
19 if( t == null )
20 {
21 cherr <= "null Type passed to about()!" <= IO.newline();
22 return;
23 }
24
25 //-----------------------------------------------------------------
26 // use .name() to get full type name e.g., 'int[][]'
27 //-----------------------------------------------------------------
28 cherr <= "type name using .name(): " <= t.name() <= IO.newline();
29 //-----------------------------------------------------------------
30 // use .baseName() to get base type name (without the array part)
31 //-----------------------------------------------------------------
32 cherr <= "type name using .baseName(): " <= t.baseName();
33 repeat( t.arrayDepth() ) cherr <= "[]";
34 cherr <= IO.newline();
35
36 //-----------------------------------------------------------------
37 // parents
38 //-----------------------------------------------------------------
39 cherr <= "inherits: ";
40 // get parent
41 t.parent() @=> Type @ p;
42 // nothing
43 if( p == null ) cherr <= "(nothing)" <= IO.newline();
44 else
45 {
46 while( p != null )
47 {
48 // name of parent
49 cherr <= p.name();
50 // get parent's parent
51 p.parent() @=> p;
52 // null?
53 cherr <= ((p != null) ? " -> " : IO.newline());
54 }
55 }
56
57 //-----------------------------------------------------------------
58 // is a kind of?
59 //-----------------------------------------------------------------
60 cherr <= "isa...Object:" <= t.isa("Object") <= " "
61 <= "UGen:" <= t.isa("UGen") <= " "
62 <= "UAna:" <= t.isa("UAna") <= IO.newline();
63
64 //-----------------------------------------------------------------
65 // more attributes
66 //-----------------------------------------------------------------
67 cherr <= "primitive: " <= (t.isPrimitive() ? "YES" : "NO" ) <= IO.newline();
68 cherr <= "array: " <= (t.isArray() ? "YES" : "NO" )
69 <= " depth: " <= t.arrayDepth() <= IO.newline();
70 }
71
72 // make an array
73 int array[2][2];
74 about( array.typeOf() );
75 about( Type.of( array ) );
76
77 // a patch
78 SinOsc foo;
79
80 // get the type of classes
81 SinOsc.typeOf() @=> Type @ tSinOsc;
82 // print info
83 about( tSinOsc );
84 // get the type of an instance
85 foo.typeOf() @=> Type @ tfoo;
86 // print info (should be the same as above)
87 about( tfoo );
88
89 // get type of any value or variable
90 about( Type.of( 1 ) );
91 about( Type.of( 2.0 ) );
92 about( Type.of( now ) );
93 about( Type.of( 3::second ) );
94 about( Type.of( #(1,2) ) );
95 about( Type.of( %(1,pi/2) ) );
96 about( Type.of( @(1,2,3) ) );
97 about( Type.of( @(1,2,3,4) ) );
98 about( Type.of( dac ) );
99
100 // get type by name
101 about( Type.find("MFCC") );
102
103 // get the type of the Type type; should be itself
104 Type.typeOf() @=> Type @ tType;
105
106 // get all subclasses of StkInstrument
107 StkInstrument.typeOf().children() @=> Type kids[];
108 // print results
109 for( int i; i < kids.size(); i++ )
110 {
111 cherr <= "StkInstrument: " <= kids[i].name() <= IO.newline();
112 }
113
114 // get all subclasses of UAna
115 UAna.typeOf().children() @=> kids;
116 // print results
117 for( int i; i < kids.size(); i++ )
118 {
119 cherr <= "UAna: " <= kids[i].name() <= IO.newline();
120 }
121
122 // instantiate a SinOsc; assign reference to a parent class
123 SinOsc theChild @=> UGen @ theParent;
124 // static typing: should be `SinOsc UGen`
125 <<< theChild.typeOf().name(),
126 theParent.typeOf().name() >>>;
127 // instanced typing: should be `SinOsc SinOsc`
128 <<< theChild.typeOfInstance().name(),
129 theParent.typeOfInstance().name() >>>;