SCDoc: Use proper static string constants instead of comparing string literals.
[supercollider.git] / Help / Language / Literals.html
blob3a755bdaba55b2e0a603f160abf083ace17f04e8
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta http-equiv="Content-Style-Type" content="text/css">
6 <title></title>
7 <meta name="Generator" content="Cocoa HTML Writer">
8 <meta name="CocoaVersion" content="824.42">
9 <style type="text/css">
10 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica; min-height: 22.0px}
12 p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
13 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco}
15 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; min-height: 16.0px}
16 span.s1 {font: 12.0px Helvetica}
17 span.Apple-tab-span {white-space:pre}
18 </style>
19 </head>
20 <body>
21 <p class="p1"><b>Literals</b></p>
22 <p class="p2"><br></p>
23 <p class="p3">Literals are values which have a direct syntactic representation.<span class="Apple-converted-space"> </span></p>
24 <p class="p3">The following sections describe the types of literals that can be represented.</p>
25 <p class="p4"><br></p>
26 <p class="p1"><b>Numbers</b></p>
27 <p class="p4"><br></p>
28 <p class="p3">An integer is any series of digits optionally preceeded by a minus sign.</p>
29 <p class="p3">examples of integers :</p>
30 <p class="p5"><span class="Apple-tab-span"> </span>-13 <span class="Apple-converted-space">     </span></p>
31 <p class="p5"><span class="Apple-tab-span"> </span>666 <span class="Apple-converted-space">     </span></p>
32 <p class="p5"><span class="Apple-tab-span"> </span>2112 <span class="Apple-converted-space">     </span></p>
33 <p class="p5"><span class="Apple-tab-span"> </span>96</p>
34 <p class="p4"><br></p>
35 <p class="p3">A float is one or more decimal digits followed by a decimal point followed by one or more decimal digits.<span class="Apple-converted-space"> </span></p>
36 <p class="p3">You must have digits on both sides of the decimal point.<span class="Apple-converted-space">  </span>This distinguishes floating point numbers from<span class="Apple-converted-space"> </span></p>
37 <p class="p3">integer expressions like:</p>
38 <p class="p4"><br></p>
39 <p class="p5"><span class="s1"><span class="Apple-tab-span"> </span></span>8.rand</p>
40 <p class="p4"><span class="Apple-converted-space"> </span></p>
41 <p class="p3">examples of floats :</p>
42 <p class="p5"><span class="Apple-tab-span"> </span>0.39 <span class="Apple-converted-space">     </span></p>
43 <p class="p5"><span class="Apple-tab-span"> </span>98.6 <span class="Apple-converted-space">     </span></p>
44 <p class="p5"><span class="Apple-tab-span"> </span>1.0 <span class="Apple-converted-space">     </span></p>
45 <p class="p5"><span class="Apple-tab-span"> </span>-0.5<span class="Apple-converted-space">     </span></p>
46 <p class="p6"><br></p>
47 <p class="p3">Exponential notation is also supported.</p>
48 <p class="p6"><br></p>
49 <p class="p5"><span class="Apple-tab-span"> </span>1.2e4</p>
50 <p class="p5"><span class="Apple-tab-span"> </span>1e-4</p>
51 <p class="p4"><br></p>
52 <p class="p3">The constant pi can be appended to a number to create floating point constant:</p>
53 <p class="p4"><br></p>
54 <p class="p5"><span class="Apple-tab-span"> </span>2pi <span class="Apple-converted-space">   </span></p>
55 <p class="p5"><span class="Apple-tab-span"> </span>0.5pi</p>
56 <p class="p5"><span class="Apple-tab-span"> </span>-0.25pi</p>
57 <p class="p6"><span class="Apple-tab-span"> </span></p>
58 <p class="p3">Numbers can also be written in radices other than base 10 up to base 36.<span class="Apple-converted-space"> </span></p>
59 <p class="p3">The radix is specified in base 10 followed by the letter 'r' followed by the value written in</p>
60 <p class="p3">that radix using characters 0-9,A-Z, or a-z, for digit values from 0 to 35.<span class="Apple-converted-space"> </span></p>
61 <p class="p3">For example you can write hexidecimal numbers as follows:</p>
62 <p class="p4"><br></p>
63 <p class="p5">16rF0</p>
64 <p class="p5">16rA9FF</p>
65 <p class="p4"><br></p>
66 <p class="p3">Binary numbers can be written as follows:</p>
67 <p class="p6"><br></p>
68 <p class="p5">2r01101011</p>
69 <p class="p6"><br></p>
70 <p class="p3">Floating point values may also be specified in any base:</p>
71 <p class="p4"><br></p>
72 <p class="p5">12r4A.A</p>
73 <p class="p4"><br></p>
74 <p class="p1"><b>Characters</b></p>
75 <p class="p4"><br></p>
76 <p class="p3">Characters are preceeded by a dollar sign:</p>
77 <p class="p3"><span class="Apple-tab-span"> </span>$A<span class="Apple-converted-space">     </span></p>
78 <p class="p3"><span class="Apple-tab-span"> </span>$B<span class="Apple-converted-space">     </span></p>
79 <p class="p3"><span class="Apple-tab-span"> </span>$C<span class="Apple-converted-space">       </span></p>
80 <p class="p4"><br></p>
81 <p class="p3">Tab, linefeed, carriage return, and backslash are preceeded by a backslash:</p>
82 <p class="p5"><span class="Apple-tab-span"> </span>$\t</p>
83 <p class="p5"><span class="Apple-tab-span"> </span>$\n</p>
84 <p class="p5"><span class="Apple-tab-span"> </span>$\r</p>
85 <p class="p5"><span class="Apple-tab-span"> </span>$\\</p>
86 <p class="p6"><br></p>
87 <p class="p6"><br></p>
88 <p class="p1"><b>Symbols</b><span class="s1"><span class="Apple-converted-space"> </span></span></p>
89 <p class="p4"><br></p>
90 <p class="p3">A symbol is written as a string enclosed in single quotes.<span class="Apple-converted-space"> </span></p>
91 <p class="p3">examples of symbols:</p>
92 <p class="p4"><br></p>
93 <p class="p5"><span class="Apple-tab-span"> </span>'x'</p>
94 <p class="p5"><span class="Apple-tab-span"> </span>'aiff'</p>
95 <p class="p5"><span class="Apple-tab-span"> </span>'BigSwiftyAndAssoc'</p>
96 <p class="p5"><span class="Apple-tab-span"> </span>'nowhere here' <span class="Apple-converted-space"> </span></p>
97 <p class="p5"><span class="Apple-tab-span"> </span>'somewhere there'<span class="Apple-converted-space"> </span></p>
98 <p class="p5"><span class="Apple-tab-span"> </span>'.+o*o+.'</p>
99 <p class="p6"><span class="Apple-tab-span"> </span></p>
100 <p class="p3">A symbol consisting of a single word can be written with a preceeding backslash.</p>
101 <p class="p6"><br></p>
102 <p class="p5"><span class="Apple-tab-span"> </span>\x</p>
103 <p class="p5"><span class="Apple-tab-span"> </span>\aiff</p>
104 <p class="p5"><span class="Apple-tab-span"> </span>\BigSwiftyAndAssoc</p>
105 <p class="p4"><br></p>
106 <p class="p1"><b>Strings</b></p>
107 <p class="p4"><br></p>
108 <p class="p3">Strings are written in double quotes:</p>
109 <p class="p4"><br></p>
110 <p class="p5"><span class="Apple-tab-span"> </span>"This is a string."</p>
111 <p class="p4"><span class="Apple-tab-span"> </span></p>
112 <p class="p3">If two or more strings are lexically adjacent, then they combine into a larger string.<span class="Apple-converted-space"> </span></p>
113 <p class="p4"><br></p>
114 <p class="p3">example:</p>
115 <p class="p4"><br></p>
116 <p class="p5"><span class="Apple-tab-span"> </span>"This" " is " "also a " "string."</p>
117 <p class="p4"><br></p>
118 <p class="p3">Strings may span more than one line. If so, then the new line characters become part of the string.</p>
119 <p class="p3">example:</p>
120 <p class="p4"><br></p>
121 <p class="p5">"This</p>
122 <p class="p5">is<span class="Apple-converted-space"> </span></p>
123 <p class="p5">also a</p>
124 <p class="p5">string.</p>
125 <p class="p5">"</p>
126 <p class="p4"><span class="Apple-tab-span"> </span></p>
127 <p class="p1"><b>Identifiers</b><span class="s1"><span class="Apple-converted-space"> </span></span></p>
128 <p class="p4"><br></p>
129 <p class="p3">Names of methods and variables begin with a lower case alphabetic character, followed<span class="Apple-converted-space">  </span>by zero or more<span class="Apple-converted-space"> </span></p>
130 <p class="p3">alphanumeric characters.<span class="Apple-converted-space"> </span></p>
131 <p class="p4"><br></p>
132 <p class="p5">var abc, z123, func;</p>
133 <p class="p6"><br></p>
134 <p class="p4"><br></p>
135 <p class="p1"><b>Class Names</b></p>
136 <p class="p4"><br></p>
137 <p class="p3">Class names always begin with a capital letter followed<span class="Apple-converted-space">  </span>by zero or more<span class="Apple-converted-space">  </span>alphanumeric characters.</p>
138 <p class="p4"><br></p>
139 <p class="p5">Object</p>
140 <p class="p5">Point</p>
141 <p class="p5">Synth</p>
142 <p class="p4"><br></p>
143 <p class="p4"><br></p>
144 <p class="p1"><b>Special Values</b></p>
145 <p class="p4"><br></p>
146 <p class="p3">The singular instances of the classes True, False and Nil are written as the<span class="Apple-converted-space"> </span></p>
147 <p class="p3">words true, false, nil and inf.</p>
148 <p class="p4"><br></p>
149 <p class="p5">x = true;</p>
150 <p class="p5">y = false;</p>
151 <p class="p5">z = nil;</p>
152 <p class="p6"><br></p>
153 <p class="p1"><b>Literal Arrays</b></p>
154 <p class="p4"><br></p>
155 <p class="p3">Arrays of literals are created at compile time and are written with a # preceeding the array as follows:</p>
156 <p class="p4"><br></p>
157 <p class="p5">#[1, 2, 'abc', "def", 4]</p>
158 <p class="p6"><br></p>
159 <p class="p3">Literal Arrays must be used as is and may not be altered at run time.</p>
160 <p class="p4"><br></p>
161 <p class="p3">In literal Arrays names are interpreted as symbols. This is not the case in regular Arrays, where they are interpreted as variable names:</p>
162 <p class="p4"><br></p>
163 <p class="p5">#[foo, bar]<span class="Apple-tab-span"> </span>// this is legal; an Array of Symbols</p>
164 <p class="p5">[foo, bar]<span class="Apple-tab-span"> </span>// this is only legal if foo and bar have been declared as variables</p>
165 <p class="p4"><br></p>
166 <p class="p3">Arrays and other collections may also be created dynamically which is explained in Collections.help.</p>
167 <p class="p3">Using a literal Array is faster than building an array dynamically every time you need it.</p>
168 <p class="p6"><br></p>
169 <p class="p3">When nesting literal arrays, only the outermost literal array needs the '#' character.</p>
170 <p class="p6"><br></p>
171 <p class="p5">#[[1, 2, 3], [4, 5, 6]]</p>
172 <p class="p4"><br></p>
173 <p class="p3">Literal Arrays can be useful for things such as tables of constants, for example note names:</p>
174 <p class="p4"><br></p>
175 <p class="p5">(</p>
176 <p class="p5">// build a table of note names</p>
177 <p class="p5">var table = ();</p>
178 <p class="p5">value {</p>
179 <p class="p5"><span class="Apple-tab-span"> </span>var semitones = [0, 2, 4, 5, 7, 9, 11];</p>
180 <p class="p5"><span class="Apple-tab-span"> </span>var naturalNoteNames = ["c", "d", "e", "f", "g", "a", "b"];</p>
181 <p class="p6"><br></p>
182 <p class="p5"><span class="Apple-tab-span"> </span>(0..9).do {|o|</p>
183 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>naturalNoteNames.do {|c, i|</p>
184 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>var n = (o + 1) * 12 + semitones[i];</p>
185 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>table[(c ++ o).asSymbol] = n;</p>
186 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>table[(c ++ "s"<span class="Apple-converted-space">  </span>++ o).asSymbol] = n + 1;</p>
187 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>table[(c ++ "ss" ++ o).asSymbol] = n + 2;</p>
188 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>table[(c ++ "b"<span class="Apple-converted-space">  </span>++ o).asSymbol] = n - 1;</p>
189 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>table[(c ++ "bb" ++ o).asSymbol] = n - 2;</p>
190 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>};</p>
191 <p class="p5"><span class="Apple-tab-span"> </span>};</p>
192 <p class="p5">};</p>
193 <p class="p6"><br></p>
194 <p class="p5">// translate note names to midi keys</p>
195 <p class="p5">table.atAll(#[c4, e4, gs4, c5, e5, gs5, c6])</p>
196 <p class="p5">)</p>
197 <p class="p4"><br></p>
198 </body>
199 </html>