Reputation: 7616
When I try to assign to a name in Dyalog APL, I get one of these error messages:
SYNTAX ERROR: Can't change nameclass on assignment
SYNTAX ERROR: Invalid modified assignment, or an attempt was made to change nameclass on assignment
What exactly does it mean to "change nameclass on assignment", why isn't it allowed, and how can I work around this issue?
Upvotes: 3
Views: 252
Reputation: 7616
APL distinguishes between syntactic roles, and each role is identified by a number. The ⎕NC
function takes one or more names, and returns their Name Class, for example 2 for variable and 3 for function, 4 for operator, and 9 for ref.
As per the assignment documentation:
Re-Assignment
A name that already exists may be assigned a new value if the assignment will not alter its name class, or will change it from 2 to 9 or vice versa. The table of permitted re-assignments is as follows:
Ref Variable Function Operator Ref Yes Yes Variable Yes Yes Function Yes Yes Operator Yes Yes
The prohibition against certain re-assignments is necessary to disambiguate when an augmented assignment is performed, or when the value of an assignment is used (whatever is on the right of the assignment arrow is returned as result). For example:
Plus←+
a←100
a Plus←10
a
110
Double←{2×⍵}
Double b←10
20
Without this restriction, it would be impossible to distinguish these cases from parallel assignment:
c d←10
c
10
d
10
Reusing a name for something entirely different is probably a bad idea in production code, and adoption of a strict naming convention (e.g. mine) is recommended. However, when experimenting in an interactive session (REPL), simply removing the definition of an existing name, opens up its usage for all purposes. The ⎕EX
(expunge) system function and the )ERASE
system command both do this:
a←10
a←+
SYNTAX ERROR: Can't change nameclass on assignment
a←+
∧
⎕EX'a'
a←+
a←10
SYNTAX ERROR: Invalid modified assignment, or an attempt was made to change nameclass on assignment
a←10
∧
)erase a
a←10
Upvotes: 6