RosettaCodeData/Task/Enforced-immutability/M2000-Interpreter/enforced-immutability-1.m2000
2023-07-01 13:44:08 -04:00

127 lines
2.9 KiB
Text

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