2 summary:: values with a direct syntactic representation
3 related:: Classes/Number, Classes/Integer, Classes/Float, Classes/SimpleNumber, Classes/String, Classes/Array, Classes/Symbol
6 Literals are values which have a direct syntactic representation.
7 The following sections describe the types of literals that can be represented.
12 An integer is any series of digits optionally preceded by a minus sign:
21 A float is one or more decimal digits followed by a decimal point followed by one or more decimal digits.
22 You must have digits on both sides of the decimal point. This distinguishes floating point numbers from
23 integer expressions like code::8.rand::.
33 Exponential notation is also supported:
39 The constant pi can be appended to a number to create a floating point constant:
48 Numbers can also be written in radices other than base 10 up to base 36.
49 The radix is specified in base 10 followed by the letter 'r' followed by the value written in
50 that radix using characters 0-9,A-Z, or a-z, for digit values from 0 to 35.
51 For example you can write hexidecimal numbers as follows:
56 Binary numbers can be written as follows:
61 Floating point values may also be specified in any base:
68 Characters are preceded by a dollar sign:
75 Tab, linefeed, carriage return, and backslash are preceded by a backslash:
85 A symbol is written as a string enclosed in single quotes.
96 A symbol consisting of a single word can be written with a preceeding backslash.
105 Strings are written in double quotes:
110 If two or more strings are lexically adjacent, then they combine into a larger string:
112 "This" " is " "also a " "string."
115 Strings may span more than one line. If so, then the new line characters become part of the string:
126 Names of methods and variables begin with a lower case alphabetic character, followed by zero or more
127 alphanumeric characters:
134 Class names always begin with a capital letter followed by zero or more alphanumeric characters.
143 section::Special Values
145 The singular instances of the classes True, False and Nil are written as the
146 words true, false, nil and inf.
155 link::Classes/Array::s of literals are created at compile time and are written with a # preceeding the array as follows:
157 #[1, 2, 'abc', "def", 4]
159 Literal Arrays must be used as is and may not be altered at run time.
161 In literal Arrays names are interpreted as symbols. This is not the case in regular Arrays, where they are interpreted as variable names:
163 #[foo, bar] // this is legal; an Array of Symbols
164 [foo, bar] // this is only legal if foo and bar have been declared as variables
166 Arrays and other collections may also be created dynamically which is explained in link::Classes/Collection::.
167 Using a literal Array is faster than building an array dynamically every time you need it.
169 When nesting literal arrays, only the outermost literal array needs the '#' character.
171 #[[1, 2, 3], [4, 5, 6]]
174 Literal Arrays can be useful for things such as tables of constants, for example note names:
177 // build a table of note names
180 var semitones = [0, 2, 4, 5, 7, 9, 11];
181 var naturalNoteNames = ["c", "d", "e", "f", "g", "a", "b"];
184 naturalNoteNames.do {|c, i|
185 var n = (o + 1) * 12 + semitones[i];
186 table[(c ++ o).asSymbol] = n;
187 table[(c ++ "s" ++ o).asSymbol] = n + 1;
188 table[(c ++ "ss" ++ o).asSymbol] = n + 2;
189 table[(c ++ "b" ++ o).asSymbol] = n - 1;
190 table[(c ++ "bb" ++ o).asSymbol] = n - 2;
195 // translate note names to midi keys
196 table.atAll(#[c4, e4, gs4, c5, e5, gs5, c6])
200 section:: Compiler limits
202 There is no theoretical limit on the number of literals in a single function, if those literals are used as freestanding objects. (Of course, there remains the practical limits of system memory and the processor time required to keep track of all the objects.)
204 The following are a special category of literal, called emphasis::selectors::.
209 ## Function definitions (enclosed in curly braces code::{ }::)
212 Here, there are four selectors: code::SinOsc::, code::ar::, code::play:: and the entire function containing SinOsc.
214 code::{ SinOsc.ar(440, 0, 0.1) }.play;::
216 A single function may contain no more than 256 selectors. If this limit is exceeded, a compiler error is printed:
218 teletype::ERROR: Selector table too big: too many classes, method selectors or function definitions in this function. Simplify the function.::
221 Code submitted for interactive execution is first compiled into a function. A very large block of code, spanning several thousand lines, may run into this limitation if it doesn't break down the tasks into smaller functions. In general, code is easier to maintain if it is reasonably emphasis::modular::, with small functions handling clearly-defined parts of the problem. This error message is a signal that the code has become too complex for a loose or "flat" code structure.
224 subsection:: What counts as "inside the function"?
226 Selectors are counted only toward the function definition currently being compiled.
234 Both functions contain exactly one selector. They are separate functions. The use of "foo" in one function doesn't affect the number of selectors in another function.
239 if(n > 1) { n * f.value(n-1) } { 1 }
246 The outer function includes only the selector code::value::. The other selectors -- code::>::, code::*::, code::-:: -- belong to the inner function definition and don't affect the outer function's number of selectors.
248 So, one possible easy way to work around the limitation is to break up a large block of code into several functions that are value'd successively:
252 ... a bunch of code ...
256 ... a bunch of code ...
260 ... a bunch of code ...