operators & operations
- => (chuck)
- + - * / (arithmetic)
- % (modulo)
- && || == != >= <=” (logic)
- & | ^ (bitwise)
- ++ – (inc / dec)
- ! + - new (unary)
You may have seen many of the operators in other programming languages (C/Java). Some others are native to ChucK. We start with the family of ChucK operators.
=> (the ChucK operator)
The ChucK operator (=>
) is a massively(大量地) overloaded operator that, depending on the types involved(包含), performs various actions(执行各种操作). It denotes(表示) **action**, can be chained(链接), and imposes(强制执行) and clarifies order(理清顺序) (always goes *from left to right*).
The ChucK operator is the means by which work is done in ChucK. Furthermore(此外), the ChucK operator is not a single operator, but a family of operators.
=> (foundational(基础的) ChucK operator)
We start with the standard, plain-vanilla(普通的,无修饰的) ChucK operator (=>
). It is left-associative (all ChucK operators are), which allows us to specify(指定) any ordered flow of data/tasks/modules (such as unit generator connection) from left to right, as in written (English) text. What =>
does depends on the context(环境). It always depends on the type of the entity(实体) on the left (the chucker(投手)) and the one on the right (the chuckee(接球手)), and it sometimes also depends on the nature of the entity (such as whether it is a variable(变量的) or not).
Some examples:
(用了c的高亮,至少好看点)
There are many other well-defined(定义明确的) uses of the ChucK operator, depending on the context.
@=> (explicit(明确的) assignment(分配) ChucK operator)
In ChucK, there is no stardard assignment operator (=), found in many other programming languages. Assignment is carried out using ChucK operators. In the previous examples, we have used => for assignment:
The @=>
explicit assignment ChucK operator behaves exactly the same for the above types (int, float, dur, time). However, the difference is that @=>
can also be used for reference assignments(分配) of objects (see objects and classes) whereas(然而) =>
only does assignment on primitive(原始的) types (int, float, dur, time). The behavior(行为) of =>
on objects is completely context-dependent(上下文相关的).
In its own screwed-up(糟透了的) way, this is kind of nice because there is no confusion(混淆) between assignment (@=>
or =>
) and equality (==
). In fact, the following is not a valid ChucK statement(声明):
+=> -=> *=> /=> etc. (arithmetic(算数) ChucK operators)
These operators are used with variables(变量) (using ‘int’ and ‘float’) to perform one operation with assignment(执行一个有分配的操作).
It is important to note the relationship between the value and variable when using -=>
and /=>
, since these operations are not commutative(交换的).
+ - * / (arithmetic)
cast(转换)
ChucK implicitly(隐式地) casts int values to float when float is expected, but not the other around. The latter could result in a loss of information and requires an explicit(明确的) cast.
% (modulo(取模))
The modulo operator %
computes the remainder(余数) after integer(整数), floating point, duration, and time/duration division.
the latter (time/duration mod) is one of many ways to dynamically(动态地) synchronize(同步) timing in shreds. the examples otf_01.ck
through otf_07.ck
(see under examples
) make use of this to on-the-fly(运行时) synchronize its various parts, no matter when each shred is added to the virtual(虚拟的) machine:
This is one of many ways to compute and reason about(思考) time in ChucK. The appropriate(适当的) solution(解决方案)(s) in each case depends on the intended(打算的) functionality(功能).
&& || == != > >= < <= (logic)(逻辑)
Logical operators - each of these need two operands(操作数). The result is an integer value of 0 or 1.
- && : and
- || : or
- == : equals
- != : does not equal
- > : greater than
- >= : greater than or equal to
- < : less than
- <= : less than or equal to
>> << & | ^ (bitwise)(按位)
These are used on int values at the bit level, often for bit masking.
++ – (inc / dec)
Values may be incremented or decremented by appending(附加) the ++
or --
operators.
! + - new (unary(一元的))
These operators come before(位于…之前) one operand(操作数).