Enforced immutability: Difference between revisions

no edit summary
m (Oops, removed duplicate Wren header.)
No edit summary
Line 620:
{{works with|lua|5.4}}
<syntaxhighlight lang="lua">local pi <const> = 3.14159265359</syntaxhighlight>
 
=={{header|M2000 Interpreter}}==
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).
 
Here we have four big modules (in a module say A). The first show how to use final in class/group definition, and what happen if we use class inheritance. The second module show how we can produce group from merging two others (here using With operator), and what happen with final members. The third module show how we use a reference to global constant, and how we can check type. The last module show constant with lambda functions.
 
<syntaxhighlight lang="m2000 interpreter">
module inheritanceByClass {
class alfa {
// final x is an object with the value of x,
// and interpreter trait it as read only variable
final x=100
// module just marked as final
module final tryme {
Print "Can't change"
}
}
class delta as alfa {
x=500
// modules and functions can alter definitions
// by a new one, unless they have marked final
// only for modules/functions as member of groups.
module tryme {
print "I win or not ?"
}
}
z=delta()
print z.x =100
z.tryme ' can't change
}
inheritanceByClass
 
module inheritanceByInstance {
class alfa {
final x=100
module final tryme {
Print "Can't change"
}
}
class delta {
x=500
module tryme {
print "I win or not ?"
}
}
 
z1=delta() with alfa()
// x is final, because delta be on top of alfa
print z1.x=100
try {
z1.x++
}
print z1.x=100
// that didn't hold for module. The final on module, close it.
z1.tryme ' can't change
z2=alfa() with delta()
// the following statements show every public identifier we make, including those non on scope.
// use List to see what we have as variables here (including members of z1, z2)
// use List ! to render ouput using proportional character spacing
// constant values displayed inside square brackets like this [100]
list !
modules ? // use this to see what functions we have until here
// x isn't final, because alfa be on top of delta,
// because x exist as number, can't change to const object.
print z2.x=500
try {
z2.x++
}
print z2.x=501
// that didn't hold for module. The final on module, close it.
z2.tryme ' can't change
}
inheritanceByInstance
 
Module ConstantGlobal {
global const p2 as single=1.57096
module inner {
const p2 // raise error if no global const p2 exist
print p2, type$(p2)="Constant"
def type(x)=type$(x)
print type(p2)="Single" // true
}
Inner
}
ConstantGlobal
module checkLambdaConstant {
const a=lambda (x)->x**2
print a(3)=9
try {
a=lambda (x)->x*5
}
print a(3)=9 // we can't copy to a a new lambda
module checkhere (z) {
print z(3)=9
try {
z=lambda (x)->x*5
}
print z(3)=15
}
// pass by copy of a, but not as constant
checkhere a
// assign to z a copy of a, but not as constant
z=a
print z(3)=9
try {
z=lambda (x)->x*5
}
print z(3)=15 // true
// redefinition of checkhere
module checkhere {
const z=stackitem() ' get a copy of top of stack
drop ' drop top of stack
print z(3)=9
try {
z=lambda (x)->x*5
}
print z(3)=9 // z not changed now
}
// now we pass a copy, but internal we make a constant lambda
checkhere a
// using by ref pass we send the const object, not a copy of it
// actually we send a weak reference, and at Read &z,
// the Read statement (Interpreter insert it automatic), make the link.
module checkByRef(&z) {
print z(3)=9
try {
z=lambda (x)->x*5
}
print z(3)=9 // z not changed
}
checkByRef &a
}
checkLambdaConstant
 
</syntaxhighlight>
 
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
404

edits