1 <!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
4 <meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8">
5 <meta http-equiv=
"Content-Style-Type" content=
"text/css">
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: 12.0px Helvetica
; min-height: 14.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 Monaco
}
14 p
.p5
{margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco
; min-height: 16.0px}
15 p
.p6
{margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco
; color: #a71e12}
16 span
.s1
{color: #a71e12}
17 span
.s2
{color: #000000}
21 <p class=
"p1"><b>Assignment Statements
<span class=
"Apple-converted-space"> </span></b></p>
22 <p class=
"p2"><br></p>
23 <p class=
"p2"><br></p>
24 <p class=
"p3"><b>Single Assignment
</b></p>
25 <p class=
"p2"><br></p>
26 <p class=
"p3">A single assignment assigns the value of an expression on the right hand side to a variable on the left hand side. A single assignment is in the form:
</p>
27 <p class=
"p2"><br></p>
28 <p class=
"p3"><variable
> =
<an expression
></p>
29 <p class=
"p2"><br></p>
30 <p class=
"p3">examples:
</p>
31 <p class=
"p2"><br></p>
32 <p class=
"p4">x = [
1,
2,
3,
4].rotate(
1);
</p>
33 <p class=
"p4">c = a + b;
</p>
34 <p class=
"p2"><br></p>
35 <p class=
"p2"><br></p>
36 <p class=
"p3"><b>Multiple Assignment
</b></p>
37 <p class=
"p2"><br></p>
38 <p class=
"p3">A multiple assignment statement assigns the elements of a Collection which is the result of an expression on the right hand side, to a list of variables on the left hand side. A multiple assignment statement is preceeded by the symbol #. If the last variable on the left is preceeded by three dots, then the entire remainder of the collection is assigned to that variable. There must be at least one variable name before the ellipsis.
</p>
39 <p class=
"p2"><br></p>
40 <p class=
"p3">The form of a multiple assignment is:
</p>
41 <p class=
"p2"><br></p>
42 <p class=
"p3">#
<list of variables
> =
<expression
></p>
43 <p class=
"p3"><span class=
"Apple-converted-space"> </span>-- or --
</p>
44 <p class=
"p3">#
<list of variables
> ...
<variable
> =
<expression
></p>
45 <p class=
"p2"><br></p>
46 <p class=
"p3">examples:
</p>
47 <p class=
"p2"><br></p>
48 <p class=
"p4"># a, b, c = [
1,
2,
3,
4,
5,
6];
<span class=
"s1">// afterwards a=
1, b=
2, c=
3</span></p>
49 <p class=
"p5"><br></p>
50 <p class=
"p6"><span class=
"s2"># a, b ... c = [
1,
2,
3,
4,
5,
6];
</span>// afterwards a=
1, b=
2, c = [
3,
4,
5,
6]
</p>
51 <p class=
"p5"><br></p>
52 <p class=
"p6"><span class=
"s2"># ... a = [
1,
2,
3,
4,
5,
6];
</span>// ILLEGAL, just use:
<span class=
"Apple-converted-space"> </span>a = [
1,
2,
3,
4,
5,
6];
</p>
53 <p class=
"p2"><br></p>
54 <p class=
"p3">Multiple assignment is implemented using the 'at' method and the 'copyToEnd' method.
</p>
55 <p class=
"p3">Your right hand side expression can return any object that responds to these messages.
</p>
56 <p class=
"p2"><br></p>
57 <p class=
"p2"><br></p>
58 <p class=
"p3"><b>Instance Variable Assignment
</b></p>
59 <p class=
"p2"><br></p>
60 <p class=
"p3">The basic syntax for setting the value of an instance variable is to use the variable's setter method which is the name of the variable with an underscore appended.
</p>
61 <p class=
"p2"><br></p>
62 <p class=
"p6"><span class=
"s2">point.x_(
5);
</span>// set point's x coordinate to
5</p>
63 <p class=
"p2"><br></p>
64 <p class=
"p3">An alternative syntax is to use instance variable assignment.
</p>
65 <p class=
"p2"><br></p>
66 <p class=
"p4">point.x =
5;
</p>
67 <p class=
"p2"><br></p>
68 <p class=
"p3">This type of assignment is translated to the first form by the compiler. The two syntaxes are equivalent.
</p>
69 <p class=
"p2"><br></p>
70 <p class=
"p2"><br></p>
71 <p class=
"p3"><b>Series Assignment to an ArrayedCollection or List
</b></p>
72 <p class=
"p2"><br></p>
73 <p class=
"p3">There is a special syntax for doing assignments to a range of values in an ArrayedCollection or List.
</p>
74 <p class=
"p2"><span class=
"Apple-converted-space"> </span></p>
75 <p class=
"p4">a = (
0,
10.
.200);
</p>
76 <p class=
"p6"><span class=
"s2">a[
5.
.10] =
1;
<span class=
"Apple-converted-space"> </span></span>// series stepping by
1</p>
77 <p class=
"p5"><br></p>
78 <p class=
"p4">a = (
0,
10.
.200);
</p>
79 <p class=
"p6"><span class=
"s2">a[
7,
9.
.13] =
1;
</span>// a series by any step size
</p>
80 <p class=
"p5"><br></p>
81 <p class=
"p4">a = (
0,
10.
.200);
</p>
82 <p class=
"p6"><span class=
"s2">a[.
.5] =
1;
<span class=
"Apple-converted-space"> </span></span>// from zero to n
</p>
83 <p class=
"p5"><br></p>
84 <p class=
"p4">a = (
0,
10.
.200);
</p>
85 <p class=
"p6"><span class=
"s2">a[
12..] =
1;
<span class=
"Apple-converted-space"> </span></span>// from n to the end of the array
</p>
86 <p class=
"p5"><br></p>
87 <p class=
"p4">a = (
0,
10.
.200);
</p>
88 <p class=
"p6"><span class=
"s2">a[
1,
3..] =
1;
<span class=
"Apple-converted-space"> </span></span>// a series to the end of the array
</p>
89 <p class=
"p5"><br></p>
90 <p class=
"p2"><br></p>
91 <p class=
"p2"><span class=
"Apple-converted-space"> </span></p>
92 <p class=
"p2"><span class=
"Apple-converted-space"> </span></p>