Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
3
Task/Enforced-immutability/00-META.yaml
Normal file
3
Task/Enforced-immutability/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Enforced_immutability
|
||||
note: Initialization
|
||||
4
Task/Enforced-immutability/00-TASK.txt
Normal file
4
Task/Enforced-immutability/00-TASK.txt
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
;Task:
|
||||
Demonstrate any means your language has to prevent the modification of values, or to create objects that cannot be modified after they have been created.
|
||||
<br><br>
|
||||
|
||||
1
Task/Enforced-immutability/11l/enforced-immutability.11l
Normal file
1
Task/Enforced-immutability/11l/enforced-immutability.11l
Normal file
|
|
@ -0,0 +1 @@
|
|||
-V min_size = 10
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
List:
|
||||
byte $01,$02,$03,$04,$05
|
||||
|
|
@ -0,0 +1 @@
|
|||
bit_7 equ $80 ;every instance of "bit_7" in your source code is swapped with hexadecimal 80 during assembly
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
bit7 equ %10000000
|
||||
bit6 equ %01000000
|
||||
|
||||
MOVE.B (A0),D0
|
||||
AND.B #bit7,D0
|
||||
;D0.B contains either $00 or $80
|
||||
1
Task/Enforced-immutability/8th/enforced-immutability.8th
Normal file
1
Task/Enforced-immutability/8th/enforced-immutability.8th
Normal file
|
|
@ -0,0 +1 @@
|
|||
123 const var, one-two-three
|
||||
|
|
@ -0,0 +1 @@
|
|||
(defconst *pi-approx* 22/7)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
INT max allowed = 20;
|
||||
REAL pi = 3.1415 9265; # pi is constant that the compiler will enforce #
|
||||
REF REAL var = LOC REAL; # var is a constant pointer to a local REAL address #
|
||||
var := pi # constant pointer var has the REAL value referenced assigned pi #
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
Foo : constant := 42;
|
||||
Foo : constant Blahtype := Blahvalue;
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
type T is limited private; -- inner structure is hidden
|
||||
X, Y: T;
|
||||
B: Boolean;
|
||||
-- The following operations do not exist:
|
||||
X := Y; -- illegal (cannot be compiled
|
||||
B := X = Y; -- illegal
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
MyData := new FinalBox("Immutable data")
|
||||
MsgBox % "MyData.Data = " MyData.Data
|
||||
MyData.Data := "This will fail to set"
|
||||
MsgBox % "MyData.Data = " MyData.Data
|
||||
|
||||
Class FinalBox {
|
||||
__New(FinalValue) {
|
||||
ObjInsert(this, "proxy",{Data:FinalValue})
|
||||
}
|
||||
; override the built-in methods:
|
||||
__Get(k) {
|
||||
return, this["proxy",k]
|
||||
}
|
||||
__Set(p*) {
|
||||
return
|
||||
}
|
||||
Insert(p*) {
|
||||
return
|
||||
}
|
||||
Remove(p*) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
CONST x = 1
|
||||
|
|
@ -0,0 +1 @@
|
|||
#define x 1
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
DEF FNconst = 2.71828182845905
|
||||
PRINT FNconst
|
||||
FNconst = 1.234 : REM Reports 'Syntax error'
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
myVar=immutable (m=mutable) immutable;
|
||||
changed:?(myVar.m);
|
||||
lst$myVar
|
||||
22
Task/Enforced-immutability/C++/enforced-immutability-1.cpp
Normal file
22
Task/Enforced-immutability/C++/enforced-immutability-1.cpp
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#include <iostream>
|
||||
|
||||
class MyOtherClass
|
||||
{
|
||||
public:
|
||||
const int m_x;
|
||||
MyOtherClass(const int initX = 0) : m_x(initX) { }
|
||||
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
MyOtherClass mocA, mocB(7);
|
||||
|
||||
std::cout << mocA.m_x << std::endl; // displays 0, the default value given for MyOtherClass's constructor.
|
||||
std::cout << mocB.m_x << std::endl; // displays 7, the value we provided for the constructor for mocB.
|
||||
|
||||
// Uncomment this, and the compile will fail; m_x is a const member.
|
||||
// mocB.m_x = 99;
|
||||
|
||||
return 0;
|
||||
}
|
||||
11
Task/Enforced-immutability/C++/enforced-immutability-2.cpp
Normal file
11
Task/Enforced-immutability/C++/enforced-immutability-2.cpp
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
class MyClass
|
||||
{
|
||||
private:
|
||||
int x;
|
||||
|
||||
public:
|
||||
int getX() const
|
||||
{
|
||||
return x;
|
||||
}
|
||||
};
|
||||
|
|
@ -0,0 +1 @@
|
|||
set -r PIE = APPLE
|
||||
|
|
@ -0,0 +1 @@
|
|||
readonly DateTime now = DateTime.Now;
|
||||
|
|
@ -0,0 +1 @@
|
|||
const int Max = 100;
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
public void Method(in int x) {
|
||||
x = 5; //Compile error
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
public void Method() {
|
||||
const double sqrt5 = 2.236;
|
||||
...
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
public string Key { get; }
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
public readonly struct Point
|
||||
{
|
||||
public Point(int x, int y) => (X, Y) = (x, y);
|
||||
|
||||
public int X { get; }
|
||||
public int Y { get; }
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
public struct Vector
|
||||
{
|
||||
public readonly int Length => 3;
|
||||
}
|
||||
3
Task/Enforced-immutability/C/enforced-immutability-1.c
Normal file
3
Task/Enforced-immutability/C/enforced-immutability-1.c
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
#define PI 3.14159265358979323
|
||||
#define MINSIZE 10
|
||||
#define MAXSIZE 100
|
||||
19
Task/Enforced-immutability/C/enforced-immutability-2.c
Normal file
19
Task/Enforced-immutability/C/enforced-immutability-2.c
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
const char foo = 'a';
|
||||
const double pi = 3.14159;
|
||||
const double minsize = 10;
|
||||
const double maxsize = 10;
|
||||
|
||||
// On pointers
|
||||
const int * ptrToConst; // The value is constant, but the pointer may change.
|
||||
int const * ptrToConst; // The value is constant, but the pointer may change. (Identical to the above.)
|
||||
int * const constPtr; // The pointer is constant, but the value may change.
|
||||
int const * const constPtrToConst; // Both the pointer and value are constant.
|
||||
|
||||
// On parameters
|
||||
int main(const int argc, // note that here, the "const", applied to the integer argument itself,
|
||||
// is kind of pointless, as arguments are passed by value, so
|
||||
// it does not affect any code outside of the function
|
||||
const char** argv)
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
ENVIRONMENT DIVISION.
|
||||
CONFIGURATION SECTION.
|
||||
SPECIAL-NAMES.
|
||||
SYMBOLIC CHARACTERS NUL IS 0, TAB IS 9.
|
||||
|
|
@ -0,0 +1 @@
|
|||
01 Foo CONSTANT AS "Foo".
|
||||
|
|
@ -0,0 +1 @@
|
|||
78 Foo VALUE "Foo".
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
CONSTANT SECTION.
|
||||
01 Foo VALUE "Foo".
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
user> (def d [1 2 3 4 5]) ; immutable vector
|
||||
#'user/d
|
||||
user> (assoc d 3 7)
|
||||
[1 2 3 7 5]
|
||||
user> d
|
||||
[1 2 3 4 5]
|
||||
67
Task/Enforced-immutability/D/enforced-immutability.d
Normal file
67
Task/Enforced-immutability/D/enforced-immutability.d
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
import std.random;
|
||||
|
||||
// enum allows to define manifest (compile-time) constants:
|
||||
int sqr(int x) { return x ^^ 2; }
|
||||
enum int x = 5;
|
||||
enum y = sqr(5); // Forces Compile-Time Function Evaluation (CTFE).
|
||||
|
||||
// enums are compile-time constants:
|
||||
enum MyEnum { A, B, C }
|
||||
|
||||
|
||||
// immutable defines values that can't change:
|
||||
immutable double pi = 3.1415;
|
||||
|
||||
|
||||
// A module-level immutable storage class variable that's not
|
||||
// explicitly initialized can be initialized by its constructor,
|
||||
// otherwise its value is the default initializer during its life-time.
|
||||
|
||||
immutable int z;
|
||||
|
||||
static this() {
|
||||
z = uniform(0, 100); // Run-time initialization.
|
||||
}
|
||||
|
||||
class Test1 {
|
||||
immutable int w;
|
||||
|
||||
this() {
|
||||
w = uniform(0, 100); // Run-time initialization.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// The items array can't be immutable here.
|
||||
// "in" is short for "const scope":
|
||||
void foo(const scope int[] items) {
|
||||
// items is constant here.
|
||||
// items[0] = 100; // Cannot modify const expression.
|
||||
}
|
||||
|
||||
|
||||
struct Test2 {
|
||||
int x_; // Mutable.
|
||||
@property int x() { return this.x_; }
|
||||
}
|
||||
|
||||
// Unlike C++, D const and immutable are transitive.
|
||||
// And there is also "inout". See D docs.
|
||||
|
||||
void main() {
|
||||
int[] data = [10, 20, 30];
|
||||
foo(data);
|
||||
data[0] = 100; // But data is mutable here.
|
||||
|
||||
// Currently manifest constants like arrays and associative arrays
|
||||
// are copied in-place every time they are used:
|
||||
enum array = [1, 2, 3];
|
||||
foo(array);
|
||||
|
||||
auto t = Test2(100);
|
||||
auto x2 = t.x; // Reading x is allowed.
|
||||
assert(x2 == 100);
|
||||
|
||||
// Not allowed, the setter property is missing:
|
||||
// t.x = 10; // Error: not a property t.x
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
const
|
||||
STR1 = 'abc'; // regular constant
|
||||
STR2: string = 'def'; // typed constant
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
let pi = 3.14
|
||||
let helloWorld = "Hello, world!"
|
||||
|
|
@ -0,0 +1 @@
|
|||
let sequence = [1,2,3,4,5]
|
||||
3
Task/Enforced-immutability/E/enforced-immutability-1.e
Normal file
3
Task/Enforced-immutability/E/enforced-immutability-1.e
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
def x := 1
|
||||
|
||||
x := 2 # this is an error
|
||||
5
Task/Enforced-immutability/E/enforced-immutability-2.e
Normal file
5
Task/Enforced-immutability/E/enforced-immutability-2.e
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
var y := 1
|
||||
|
||||
def things :DeepFrozen := [&x, 2, 3] # This is OK
|
||||
|
||||
def funnyThings :DeepFrozen := [&y, 2, 3] # Error: y's slot is not immutable
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
open unsafe.cell
|
||||
r = ref 0
|
||||
|
|
@ -0,0 +1 @@
|
|||
mutate r 1
|
||||
|
|
@ -0,0 +1 @@
|
|||
valueof r
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
X = 10,
|
||||
X = 20.
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
X = 10,
|
||||
X = 10.
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
constant n = 1
|
||||
constant s = {1,2,3}
|
||||
constant str = "immutable string"
|
||||
|
|
@ -0,0 +1 @@
|
|||
let hello = "Hello!"
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
TUPLE: range
|
||||
{ from read-only } { length read-only } { step read-only } ;
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
256 constant one-hex-dollar
|
||||
s" Hello world" 2constant hello \ "hello" holds the address and length of an anonymous string.
|
||||
355 119 2constant ratio-pi \ 2constant can also define ratios (e.g. pi)
|
||||
3.14159265e fconstant pi
|
||||
|
|
@ -0,0 +1 @@
|
|||
real, parameter :: pi = 3.141593
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
subroutine sub1(n)
|
||||
real, intent(in) :: n
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
#define IMMUT1 32767 'constants can be created in the preprocessor
|
||||
dim as const uinteger IMMUT2 = 2222 'or explicitly declared as constants
|
||||
6
Task/Enforced-immutability/Go/enforced-immutability.go
Normal file
6
Task/Enforced-immutability/Go/enforced-immutability.go
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
package main
|
||||
|
||||
func main() {
|
||||
s := "immutable"
|
||||
s[0] = 'a'
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
pi = 3.14159
|
||||
msg = "Hello World"
|
||||
|
|
@ -0,0 +1 @@
|
|||
$define "1234"
|
||||
6
Task/Enforced-immutability/J/enforced-immutability-1.j
Normal file
6
Task/Enforced-immutability/J/enforced-immutability-1.j
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
B=: A=: 'this is a test'
|
||||
A=: '*' 2 3 5 7} A
|
||||
A
|
||||
th** *s*a test
|
||||
B
|
||||
this is a test
|
||||
9
Task/Enforced-immutability/J/enforced-immutability-2.j
Normal file
9
Task/Enforced-immutability/J/enforced-immutability-2.j
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
require'jmf'
|
||||
C=: 'this is a test'
|
||||
1 readonly_jmf_ 'C'
|
||||
|
||||
C =: 'some new value'
|
||||
|read-only data
|
||||
| C =:'some new value'
|
||||
C
|
||||
this is a test
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
final int immutableInt = 4;
|
||||
int mutableInt = 4;
|
||||
mutableInt = 6; //this is fine
|
||||
immutableInt = 6; //this is an error
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
final String immutableString = "test";
|
||||
immutableString = new String("anotherTest"); //this is an error
|
||||
final StringBuffer immutableBuffer = new StringBuffer();
|
||||
immutableBuffer.append("a"); //this is fine and it changes the state of the object
|
||||
immutableBuffer = new StringBuffer("a"); //this is an error
|
||||
25
Task/Enforced-immutability/Java/enforced-immutability-3.java
Normal file
25
Task/Enforced-immutability/Java/enforced-immutability-3.java
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
public class Immute{
|
||||
private final int num;
|
||||
private final String word;
|
||||
private final StringBuffer buff; //still mutable inside this class, but there is no access outside this class
|
||||
|
||||
public Immute(int num){
|
||||
this.num = num;
|
||||
word = num + "";
|
||||
buff = new StringBuffer("test" + word);
|
||||
}
|
||||
|
||||
public int getNum(){
|
||||
return num;
|
||||
}
|
||||
|
||||
public String getWord(){
|
||||
return word; //String objects are immutable so passing the object back directly won't harm anything
|
||||
}
|
||||
|
||||
public StringBuffer getBuff(){
|
||||
return new StringBuffer(buff);
|
||||
//using "return buff" here compromises immutability, but copying the object via the constructor makes it ok
|
||||
}
|
||||
//no "set" methods are given
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
const pi = 3.1415;
|
||||
const msg = "Hello World";
|
||||
1
Task/Enforced-immutability/Jq/enforced-immutability.jq
Normal file
1
Task/Enforced-immutability/Jq/enforced-immutability.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
["a", "b"] as $a | $a[0] = 1 as $b | $a
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
const x = 1
|
||||
x = π # ERROR: invalid ridefinition of constant x
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
// version 1.1.0
|
||||
|
||||
// constant top level property
|
||||
const val N = 5
|
||||
|
||||
// read-only top level property
|
||||
val letters = listOf('A', 'B', 'C', 'D', 'E') // 'listOf' creates here a List<Char) which is immutable
|
||||
|
||||
class MyClass { // MyClass is effectively immutable because it's only property is read-only
|
||||
// and it is not 'open' so cannot be sub-classed
|
||||
// read-only class property
|
||||
val myInt = 3
|
||||
|
||||
fun myFunc(p: Int) { // parameter 'p' is read-only
|
||||
var pp = p // local variable 'pp' is mutable
|
||||
while (pp < N) { // compiler will change 'N' to 5
|
||||
print(letters[pp++])
|
||||
}
|
||||
println()
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val mc = MyClass() // 'mc' cannot be re-assigned a different object
|
||||
println(mc.myInt)
|
||||
mc.myFunc(0)
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
:- object(immutable).
|
||||
|
||||
% forbid using (complementing) categories for adding to or
|
||||
% modifying (aka hot patching) the object
|
||||
:- set_logtalk_flag(complements, deny).
|
||||
% forbid dynamically adding new predicates at runtime
|
||||
:- set_logtalk_flag(dynamic_declarations, deny).
|
||||
|
||||
:- public(foo/1).
|
||||
foo(1). % static predicate by default
|
||||
|
||||
:- private(bar/2)
|
||||
bar(2, 3). % static predicate by default
|
||||
|
||||
:- end_object.
|
||||
1
Task/Enforced-immutability/Lua/enforced-immutability.lua
Normal file
1
Task/Enforced-immutability/Lua/enforced-immutability.lua
Normal file
|
|
@ -0,0 +1 @@
|
|||
local pi <const> = 3.14159265359
|
||||
|
|
@ -0,0 +1,127 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
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
|
||||
1
Task/Enforced-immutability/MBS/enforced-immutability.mbs
Normal file
1
Task/Enforced-immutability/MBS/enforced-immutability.mbs
Normal file
|
|
@ -0,0 +1 @@
|
|||
CONSTANT INT foo=640;
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
Tau = 2*Pi;Protect[Tau]
|
||||
{"Tau"}
|
||||
|
||||
Tau = 2
|
||||
->Set::wrsym: Symbol Tau is Protected.
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
def foo = 42; // immutable by default
|
||||
mutable bar = "O'Malleys"; // mutable because you asked it to be
|
||||
7
Task/Enforced-immutability/Nim/enforced-immutability.nim
Normal file
7
Task/Enforced-immutability/Nim/enforced-immutability.nim
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
var x = "mutablefoo" # Mutable variable
|
||||
let y = "immutablefoo" # Immutable variable, at runtime
|
||||
const z = "constantfoo" # Immutable constant, at compile time
|
||||
|
||||
x[0] = 'M'
|
||||
y[0] = 'I' # Compile error: 'y[0]' cannot be assigned to
|
||||
z[0] = 'C' # Compile error: 'z[0]' cannot be assigned to
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
type im_string
|
||||
|
||||
val create : int -> im_string
|
||||
val make : int -> char -> im_string
|
||||
val of_string : string -> im_string
|
||||
val to_string : im_string -> string
|
||||
val copy : im_string -> im_string
|
||||
val sub : im_string -> int -> int -> im_string
|
||||
val length : im_string -> int
|
||||
val get : im_string -> int -> char
|
||||
val iter : (char -> unit) -> im_string -> unit
|
||||
val escaped : im_string -> im_string
|
||||
val index : im_string -> char -> int
|
||||
val contains : im_string -> char -> bool
|
||||
val print : im_string -> unit
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
type im_string = string
|
||||
|
||||
let create = String.create
|
||||
let make = String.make
|
||||
let copy = String.copy
|
||||
let sub = String.sub
|
||||
let length = String.length
|
||||
let get = String.get
|
||||
let iter = String.iter
|
||||
let escaped = String.escaped
|
||||
let index = String.index
|
||||
let contains = String.contains
|
||||
|
||||
let of_string s = s
|
||||
let to_string s = s
|
||||
let print = print_string
|
||||
12
Task/Enforced-immutability/Oforth/enforced-immutability.fth
Normal file
12
Task/Enforced-immutability/Oforth/enforced-immutability.fth
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
Object Class new: MyClass(a, b)
|
||||
|
||||
MyClass method: setA(value) value := a ;
|
||||
MyClass method: setB(value) value := b ;
|
||||
|
||||
MyClass method: initialize(v, w) self setA(v) self setB(w) ;
|
||||
|
||||
MyClass new(1, 2) // OK : An immutable object
|
||||
MyClass new(1, 2) setA(4) // KO : An immutable object can't be updated after initialization
|
||||
MyClass new(ListBuffer new, 12) // KO : Not an immutable value.
|
||||
ListBuffer new Constant new: T // KO : A constant cannot be mutable.
|
||||
Channel new send(ListBuffer new) // KO : A mutable object can't be sent into a channel.
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
define("PI", 3.14159265358);
|
||||
define("MSG", "Hello World");
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
const PI = 3.14159265358;
|
||||
const MSG = "Hello World";
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
*process source attributes xref;
|
||||
constants: Proc Options(main);
|
||||
Dcl three Bin Fixed(15) Value(3);
|
||||
Put Skip List(1/three);
|
||||
Put Skip List(1/3);
|
||||
End;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
use constant PI => 3.14159;
|
||||
use constant MSG => "Hello World";
|
||||
11
Task/Enforced-immutability/Perl/enforced-immutability-2.pl
Normal file
11
Task/Enforced-immutability/Perl/enforced-immutability-2.pl
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
use Readonly;
|
||||
|
||||
Readonly::Scalar my $pi => 3.14159;
|
||||
Readonly::Scalar my $msg => "Hello World";
|
||||
|
||||
Readonly::Array my @arr => (1, 2, 3, 4, 5);
|
||||
Readonly::Hash my %hash => (
|
||||
"a" => 1,
|
||||
"b" => 2,
|
||||
"c" => 3
|
||||
);
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">}</span>
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">str</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"immutable string"</span>
|
||||
<!--
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">constant</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">constant</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">}</span>
|
||||
<span style="color: #008080;">constant</span> <span style="color: #004080;">string</span> <span style="color: #000000;">str</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"immutable string"</span>
|
||||
<!--
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
: (de pi () 4)
|
||||
-> pi
|
||||
|
||||
: (pi)
|
||||
-> 4
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
: (set (cdr pi) 3)
|
||||
-> 3
|
||||
|
||||
: (pi)
|
||||
-> 3
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
$me = "myname"
|
||||
%age = 35
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
#i_Const1 = 11
|
||||
#i_Const2 = 3.1415
|
||||
#i_Const3 = "A'm a string"
|
||||
|
|
@ -0,0 +1,78 @@
|
|||
;Enforced immutability Variable-Class
|
||||
|
||||
Interface PBVariable ; Interface for any value of this type
|
||||
Get() ; Get the current value
|
||||
Set(Value.i) ; Set (if allowed) a new value in this variable
|
||||
ToString.s() ; Transferee the value to a string.
|
||||
Destroy() ; Destructor
|
||||
EndInterface
|
||||
|
||||
Structure PBV_Structure ; The *VTable structure
|
||||
Get.i
|
||||
Set.i
|
||||
ToString.i
|
||||
Destroy.i
|
||||
EndStructure
|
||||
|
||||
Structure PBVar
|
||||
*VirtualTable.PBV_Structure
|
||||
Value.i
|
||||
EndStructure
|
||||
|
||||
;- Functions for any PBVariable
|
||||
Procedure immutable_get(*Self.PBVar)
|
||||
ProcedureReturn *Self\Value
|
||||
EndProcedure
|
||||
|
||||
Procedure immutable_set(*Self.PBVar, N.i)
|
||||
ProcedureReturn #False
|
||||
EndProcedure
|
||||
|
||||
Procedure.s immutable_ToString(*Self.PBVar)
|
||||
ProcedureReturn Str(*Self\Value)
|
||||
EndProcedure
|
||||
|
||||
Procedure DestroyImmutabe(*Self.PBVar)
|
||||
FreeMemory(*Self)
|
||||
EndProcedure
|
||||
|
||||
;- Init an OO-Table
|
||||
DataSection
|
||||
VTable:
|
||||
Data.i @immutable_get()
|
||||
Data.i @immutable_set()
|
||||
Data.i @immutable_ToString()
|
||||
Data.i @DestroyImmutabe()
|
||||
EndDataSection
|
||||
|
||||
;- Create-Class
|
||||
Procedure CreateImmutabe(Init.i=0)
|
||||
Define *p.PBVar
|
||||
*p=AllocateMemory(SizeOf(PBVar))
|
||||
*p\VirtualTable = ?VTable
|
||||
*p\Value = Init
|
||||
ProcedureReturn *p
|
||||
EndProcedure
|
||||
|
||||
;- **************
|
||||
;- Test the Code
|
||||
|
||||
;- Initiate two Immutabe variables
|
||||
*v1.PBVariable = CreateImmutabe()
|
||||
*v2.PBVariable = CreateImmutabe(24)
|
||||
|
||||
;- Present therir content
|
||||
Debug *v1\ToString() ; = 0
|
||||
Debug *v2\ToString() ; = 24
|
||||
|
||||
;- Try to change the variables
|
||||
*v1\Set(314) ; Try to change the value, which is not permitted
|
||||
*v2\Set(7)
|
||||
|
||||
; Present the values again
|
||||
Debug Str(*v1\Get()) ; = 0
|
||||
Debug Str(*v2\Get()) ; = 24
|
||||
|
||||
;- And clean up
|
||||
*v1\Destroy()
|
||||
*v2\Destroy()
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
>>> s = "Hello"
|
||||
>>> s[0] = "h"
|
||||
|
||||
Traceback (most recent call last):
|
||||
File "<pyshell#1>", line 1, in <module>
|
||||
s[0] = "h"
|
||||
TypeError: 'str' object does not support item assignment
|
||||
26
Task/Enforced-immutability/Python/enforced-immutability-2.py
Normal file
26
Task/Enforced-immutability/Python/enforced-immutability-2.py
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
>>> class Immut(object):
|
||||
def __setattr__(self, *args):
|
||||
raise TypeError(
|
||||
"'Immut' object does not support item assignment")
|
||||
|
||||
__delattr__ = __setattr__
|
||||
|
||||
def __repr__(self):
|
||||
return str(self.value)
|
||||
|
||||
def __init__(self, value):
|
||||
# assign to the un-assignable the hard way.
|
||||
super(Immut, self).__setattr__("value", value)
|
||||
|
||||
>>> im = Immut(123)
|
||||
>>> im
|
||||
123
|
||||
>>> im.value = 124
|
||||
|
||||
Traceback (most recent call last):
|
||||
File "<pyshell#27>", line 1, in <module>
|
||||
del a.value
|
||||
File "<pyshell#23>", line 4, in __setattr__
|
||||
"'Immut' object does not support item assignment")
|
||||
TypeError: 'Immut' object does not support item assignment
|
||||
>>>
|
||||
37
Task/Enforced-immutability/REXX/enforced-immutability.rexx
Normal file
37
Task/Enforced-immutability/REXX/enforced-immutability.rexx
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
/*REXX program emulates immutable variables (as a post-computational check). */
|
||||
call immutable '$=1' /* ◄─── assigns an immutable variable. */
|
||||
call immutable ' pi = 3.14159' /* ◄─── " " " " */
|
||||
call immutable 'radius= 2*pi/4 ' /* ◄─── " " " " */
|
||||
call immutable ' r=13/2 ' /* ◄─── " " " " */
|
||||
call immutable ' d=0002 * r' /* ◄─── " " " " */
|
||||
call immutable ' f.1 = 12**2 ' /* ◄─── " " " " */
|
||||
|
||||
say ' $ =' $ /*show the variable, just to be sure. */
|
||||
say ' pi =' pi /* " " " " " " " */
|
||||
say ' radius =' radius /* " " " " " " " */
|
||||
say ' r =' r /* " " " " " " " */
|
||||
say ' d =' d /* " " " " " " " */
|
||||
|
||||
do radius=10 to -10 by -1 /*perform some faux important stuff. */
|
||||
circum=$*pi*2*radius /*some kind of impressive calculation. */
|
||||
end /*k*/ /* [↑] that should do it, by gum. */
|
||||
call immutable /* ◄═══ see if immutable variables OK. */
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
immutable: if symbol('immutable.0')=='LIT' then immutable.0= /*1st time see immutable? */
|
||||
if arg()==0 then do /* [↓] chk all immutables*/
|
||||
do __=1 for words(immutable.0); _=word(immutable.0,__)
|
||||
if value(_)==value('IMMUTABLE.!'_) then iterate /*same?*/
|
||||
call ser -12, 'immutable variable ' _ " compromised."
|
||||
end /*__*/ /* [↑] Error? ERRmsg, exit*/
|
||||
return 0 /*return and indicate A-OK.*/
|
||||
end /* [↓] immutable must have =*/
|
||||
if pos('=',arg(1))==0 then call ser -4, "no equal sign in assignment:" arg(1)
|
||||
parse arg _ '=' __; upper _; _=space(_) /*purify variable name.*/
|
||||
if symbol("_")=='BAD' then call ser -8,_ "isn't a valid variable symbol."
|
||||
immutable.0=immutable.0 _ /*add immutable var to list.*/
|
||||
interpret '__='__; call value _,__ /*assign value to a variable*/
|
||||
call value 'IMMUTABLE.!'_,__ /*assign value to bkup var. */
|
||||
return words(immutable.0) /*return number immutables. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
ser: say; say '***error***' arg(2); say; exit arg(1) /*error msg.*/
|
||||
|
|
@ -0,0 +1 @@
|
|||
(struct coordinate (x y)) ; immutable struct
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
constant $pi = 3.14159;
|
||||
constant $msg = "Hello World";
|
||||
|
||||
constant @arr = (1, 2, 3, 4, 5);
|
||||
|
|
@ -0,0 +1 @@
|
|||
constant fibonacci = 0, 1, *+* ... *;
|
||||
|
|
@ -0,0 +1 @@
|
|||
my $pi := 3 + rand;
|
||||
14
Task/Enforced-immutability/Raku/enforced-immutability-4.raku
Normal file
14
Task/Enforced-immutability/Raku/enforced-immutability-4.raku
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
sub sum (Num $x, Num $y) {
|
||||
$x += $y; # ERROR
|
||||
}
|
||||
|
||||
# Explicitly ask for pass-by-reference semantics
|
||||
sub addto (Num $x is rw, Num $y) {
|
||||
$x += $y; # ok, propagated back to caller
|
||||
}
|
||||
|
||||
# Explicitly ask for pass-by-value semantics
|
||||
sub sum (Num $x is copy, Num $y) {
|
||||
$x += $y; # ok, but NOT propagated back to caller
|
||||
$x;
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
# Project : Enforced immutability
|
||||
|
||||
x = 10
|
||||
assert( x = 10)
|
||||
assert( x = 100 )
|
||||
21
Task/Enforced-immutability/Ruby/enforced-immutability-1.rb
Normal file
21
Task/Enforced-immutability/Ruby/enforced-immutability-1.rb
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
msg = "Hello World"
|
||||
msg << "!"
|
||||
puts msg #=> Hello World!
|
||||
|
||||
puts msg.frozen? #=> false
|
||||
msg.freeze
|
||||
puts msg.frozen? #=> true
|
||||
begin
|
||||
msg << "!"
|
||||
rescue => e
|
||||
p e #=> #<RuntimeError: can't modify frozen String>
|
||||
end
|
||||
|
||||
puts msg #=> Hello World!
|
||||
msg2 = msg
|
||||
|
||||
# The object is frozen, not the variable.
|
||||
msg = "hello world" # A new object was assigned to the variable.
|
||||
|
||||
puts msg.frozen? #=> false
|
||||
puts msg2.frozen? #=> true
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
# There are two methods in the copy of the object.
|
||||
msg = "Hello World!".freeze
|
||||
msg2 = msg.clone # Copies the frozen and tainted state of obj.
|
||||
msg3 = msg.dup # It doesn't copy the status (frozen, tainted) of obj.
|
||||
puts msg2 #=> Hello World!
|
||||
puts msg3 #=> Hello World!
|
||||
puts msg2.frozen? #=> true
|
||||
puts msg3.frozen? #=> false
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
let x = 3;
|
||||
x += 2;
|
||||
|
|
@ -0,0 +1 @@
|
|||
let mut x = 3;
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
let mut x = 4;
|
||||
let y = &x;
|
||||
*y += 2 // Raises compiler error. Even though x is mutable, y is an immutable reference.
|
||||
let y = &mut x;
|
||||
*y += 2// Works
|
||||
// Note that though y is now a mutable reference, y itself is still immutable e.g.
|
||||
let mut z = 5;
|
||||
let y = &mut z; // Raises compiler error because y is already assigned to '&mut x'
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue