Enforced immutability: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 622: | Line 622: | ||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
====Four Examples for Constant/Final modifiers==== |
|||
Modules and functions in M2000 can change definitions with another definition (excluded subs and simple functions which are written at the end of module/function). So except for variables to be immutable we have to do something with modules and functions, only for members of groups (the user defined object in M2000). |
Modules and functions in M2000 can change definitions with another definition (excluded subs and simple functions which are written at the end of module/function). So except for variables to be immutable we have to do something with modules and functions, only for members of groups (the user defined object in M2000). |
||
Line 754: | Line 755: | ||
} |
} |
||
checkLambdaConstant |
checkLambdaConstant |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
====partial constants by using enumeration types==== |
|||
The value of a enumeration type may change to those of the same type (not the value type, the variable type) |
|||
<syntaxhighlight lang="m2000 interpreter"> |
|||
enum constA { |
|||
x=10 |
|||
y=30 |
|||
} |
|||
// constB get two members of constA to same list, but x and y are defined once as ConstB |
|||
enum constB { |
|||
constA, z="ok" |
|||
} |
|||
// m is a ConstB type. X value exist in ConstB |
|||
def m as ConstB=x |
|||
module inner (z as constB) { |
|||
print z=10 |
|||
try { |
|||
z=500 |
|||
} |
|||
print z=10, eval$(z)="x" |
|||
check(z) |
|||
z++ |
|||
print z=30, eval$(z)="y" |
|||
check(z) |
|||
z++ |
|||
print z="ok", eval$(z)="z" |
|||
check(z) |
|||
try { |
|||
z=30 // ok 30 exist in enum constB list |
|||
} |
|||
check(z) // z is y now |
|||
sub check(z as constB) |
|||
select enum z // like a select case but for enumeration type to check names |
|||
case x ' check name of enum |
|||
print "it is x", z |
|||
case y |
|||
print "it is y", z |
|||
case z |
|||
print "it is z", z |
|||
end select |
|||
end sub |
|||
} |
|||
inner m |
|||
</syntaxhighlight> |
|||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |