ChucK is a strongly-typed language, meaning that types are resolved(決定) at compile-time. However, it is not quite statically-typed, because the compiler/type system is a part of the ChucK virtual(虛擬的) machine, and is a runtime component(成分). This type system helps to impose(強加) PRecision(精度) and clarity(清楚) in the code, and naturally lends to organization of complex(復雜的) programs. At the same time, it is also dynamic in that changes to the type system can take place (in a well-defined(定義明確的) manner) at runtime. This dynamic(動態的) aspect(方面) forms the basis(基礎) for on-the-fly programming.
This section deals with types, values, and the declaration(聲明) and usage(使用) of variables. As in other strongly-typed programming languages, we can think of a type as associated(關聯的) behaviors(行為) of data. (For example, an ‘int’ is a type that means integer(整數), and adding two integer is defined(定義) to produce a third integer representing the sum.) Classes and objects allow us to extend(延伸) the type system with our own custom types, but we won’t cover them in this section. We will focus mainly on primitive(原始的) types here, and leave the discussion of more complex types for classes and objects.
The primitive(原始的), or intrinsic(本質的) types are those which are simple datatypes (they have no additional(附加的) data attributes(屬性)). Objects are not primitive types. Primitive types are passed by value. Primitive types cannot be extended(延伸). The primitive types in ChucK are:
int : integer(整數) (signed)float : floating point number (in ChucK, a float is by default double-precision(雙精度))time : ChucKian timedur : ChucKian duration(持續)void : (no type)complex : complex number in rectangular(矩形的) form a + bi (see [below][1])polar: complex number in polar(極面) form (see [below][1])For a summary of Operations on these types, go to operations and operators.
All other types are derived(派生) from ‘object’, either as part of the ChucK standard library, or as a new class that you create. For specification(規格), go to classes and objects.
Literal values are specified(指定) explicitly(明確地) in code and are assigned(分配) a type by the compiler(編譯器).The following are some examples of literal values:
int:42int (hexidecimal):0xaf30float:1.323dur:5.5::second//In the above code, second is an existing duration(持續) variable(變量). //For more on durations, see the [manipulating time(時間調節)][4] section.Variables(變量) are locations in memory that hold data. Variables have to be declared in ChucK before they are used. For example, to declare a variable of type int called foo:
// declare an 'int' called 'foo'int foo;We can assign(分配) a value to an existing variable by using the ChucK operator (=>
). This is one of the most commonly used operators in ChucK, it’s the way to do work and take action! (后面會討論)
It is possible to combine the two statements(聲明) into one:
// assign 2 to a new variable 'foo' of type 'int'2 => int foo;To use a variable, just refer to it by name:
// debug-print the value of foo<<< foo >>>;To update the value of foo, for example:
// multiply 'foo' by 10, assign back to 'foo'foo * 10 => foo;You can also do the above using a *=>
(mult-chuck):
Here is an example of a duration(持續):
// assign value of '5 seconds' to new variable bar5::second => dur bar;Once you have bar, you can inductively(歸納的) use it to construct new durations:
// 4 bar, a measure?4::bar => dur measure;輸出: C:/Users/abc1/Desktop>chuck test.ck 882000.000000 :(dur)
Since time is central to programming ChucK, it is important to understand time, dur, the relationship and operations between them. There is more information in the manipulating time section.
Reference(參考) types are types which inherit(繼承) from the object class. Some default reference types include:
Object : base type that all classes inherit from (directly or indirectly)所有類繼承的基本類型array : N-dimensional ordered(有序的) set of data (of the same type)Event : fundamental(基本的), extendable(可擴展的), synchronization(同步) mechanism(機制)UGen : extendable unit generator(單元發生器) base class (U = unit, Gen = generator)string : string (of characters)New classes can be created. All classes are reference types. We will leave the full discussion to the objects and classes section.
(虛數:直角坐標系,極坐標系)
Two special primitive types
are available to to represent complex (復雜的)data, such as the output (輸出)of an FFT: complex and polar. A complex number of the form a + bi can be declared as
where the #(…) syntax(語法) explicitly(明確地) denotes(表示) a complex number in rectangular form(矩形). Similarly, explicit complex numbers can be manipulated(操縱) directly:
#(5, -1.5) => complex cmp; // cmp is 5 - 1.5i#(2,3) + #(5,6) + cmp => complex sum; // sum is now 12 + 7.5iThe (floating point) real and imaginary parts(實虛部) of a complex number can be accessed(存取) with the .re and .im components of a complex number:
#(2.0,3.5) => complex cmp;cmp.re => float x; // x is 2.0cmp.im => float y; //y is 3.5The polar type(極坐標) offers an equivalent(等價的), alternative(供選擇的) representation(表示) of complex numbers in terms of a magnitude(大小) and phase(相) value. A polar representation of a complex number can be declared as
%(2, .5*pi) => polar pol; // pol is 2∠.5π(沒有想到,竟然有這個(●'?'●))The magnitude(大小) and phase(相) values can be accessed via .mag and .phase:
%(2, .5*pi) => polar pol;pol.mag => float m; // m is 2pol.phase => float p; //p is .5πpolar and complex representations(代表) can be cast to each other and multiplied/added/assigned(分配)/etc.:
%(2, .5*pi) => polar pol;#(3, 4) => complex cmp;pol $ complex + #(10, 3) + cmp => complex cmp2;cmp $ polar + %(10, .25*pi) - pol => polar pol2;(這里的$實現了兩種坐標系下表示的轉換)
輸出: C:/Users/abc1/Desktop>chuck test.ck %(2.0000,0.5000*pi) :(polar) #(3.0000,4.0000) :(complex) #(13.0000,9.0000) :(complex) %(13.5540,0.2334*pi) :(polar)
新聞熱點
疑難解答