@@ -14,15 +14,26 @@ <h1>Assignments with the <code>=</code> Operator</h1>
14
14
< p >
15
15
All the previously allowed assignment operators (< code > < - </ code > ,
16
16
< code > :=</ code > , < code > _</ code > , and < code > < < - </ code > )
17
- remain fully in effect. The < code > =</ code > operator is semantically
18
- equivalent to all of these, except the last.
17
+ remain fully in effect. The < code > _</ code > is a historical curio and
18
+ there seems to be general agreement that it should be phased out.
19
+ (The historical origin is as follows. During the early work on S at
20
+ Bell Labs, the main time-shared terminal in use was the Execuport, a
21
+ heavyweight thermal-paper machine which had, among other oddities, a
22
+ key corresponding to the ASCII underscore that printed as a back
23
+ arrow. This was adopted as an easy-to-type and natural-looking
24
+ assignment operator. The two-character back arrow was the less
25
+ convenient, but portable version.)
26
+
27
+ Where it is allowed, the < code > =</ code > operator is semantically
28
+ equivalent to all of the earlier assignments, except of course < code > < < - </ code . However, it is not
29
+ allowed in all situations.
19
30
< p >
20
31
< h3 > Limitations Imposed</ h3 >
21
32
< p >
22
33
The new assignments are allowed in only two places in the grammar: at
23
- the top level (as a complete program or user-typed expression); and as
24
- an element in a braced list of expressions. In all other places, one
25
- of the existing assignment operators must be used .
34
+ the top level (as a complete program or user-typed expression); and
35
+ when isolated from surrounding logical structure, by braces or an
36
+ extra pair of parentheses .
26
37
< p >
27
38
So the following are allowed:
28
39
< code > < pre >
@@ -31,24 +42,26 @@ <h3>Limitations Imposed</h3>
31
42
> zzz$a[1] = NA
32
43
> for(i in 1:10) { xx = min(rnorm(100)); zzz$a[i] = xx}
33
44
</ pre > </ code >
34
- But the following is not :
45
+ But the following famous C programming error is illegal in the grammar :
35
46
< code > < pre >
36
- > while((temp = y[i]) > .5) i
47
+ > if(x = 0) 1 else x
37
48
Error: syntax error
38
49
</ pre > </ code >
39
- Embedded assignments of this sort have to be done with one of the older
40
- assignment operators.
50
+ Assignments in control structures are generally dangerous; in any
51
+ case, to use them you have to either use one of the older assignment
52
+ operators, or add an extra level of parentheses.
41
53
< p >
42
54
There are two reasons for the restricitions:
43
55
< ol >
44
- < li > Some restriction is needed, because the same syntax is used for
45
- specifying named arguments in function calls. The additional
46
- restrictions are minor by comparison.< p >
56
+ < li > The language already has assignments with < code > =</ code > ,
57
+ known as named arguments in calls. These of course continue to
58
+ be interpreted as before. (They are in essence assignments in
59
+ the environment created for the call.) Regualar assignments in any
60
+ argument in a function call still have to be done with the old
61
+ operator, or else surrounded by an extra set of parentheses.< p >
47
62
< li > Disallowing the new assignment form in control expressions
48
- avoids some infamous C-style programming errors, such as
49
- < code > < pre >
50
- while(result = 0) ## do something important (but it won't happen!)
51
- </ pre > </ code >
63
+ avoids programming errors (such as the example above) that are
64
+ more likely with the equal operator than with other S assignments.
52
65
</ ol >
53
66
< p >
54
67
The restrictions do produce some other limitations that users may find
@@ -62,15 +75,17 @@ <h3>Limitations Imposed</h3>
62
75
> quote(y[1] = 1)
63
76
Error: syntax error
64
77
</ pre > </ code >
65
- ( The function is implemented as a special that doesn't check argument
78
+ The function is implemented as a special that doesn't check argument
66
79
names. The first two examples just use < code > y</ code > as the
67
80
argument name, and the third fails because the expression on the left
68
- of the < code > =</ code > operator isn't valid as an argument name.)
81
+ of the < code > =</ code > operator isn't valid as an argument name.
82
+ A future revision of the function could interpret the first two, but the
83
+ grammar pretty much rules out the last example.
69
84
70
85
71
86
< hr >
72
87
< address > < a href ="
http://cm.bell-labs.com/cm/ms/departments/sia/jmc/ "
> John Chambers
</ a > < a href =
mailto:[email protected] > <
[email protected] >
</ a > </ address >
73
88
<!-- hhmts start -->
74
- Last modified: Mon Aug 13 17:08:02 EDT 2001
89
+ Last modified: Sun Dec 16 17:58:58 EST 2001
75
90
<!-- hhmts end -->
76
91
</ body > </ html >
0 commit comments