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