September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1 +1,3 @@
|
|||
;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>
|
||||
|
|
|
|||
|
|
@ -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,5 @@
|
|||
// you can freeze any object.
|
||||
b = [1, 2, 3];
|
||||
b[1] = 100; // returns [1, 100, 3]
|
||||
b.freeze; // make b immutable
|
||||
b[1] = 2; // throws an error ("Attempted write to immutable object.")
|
||||
5
Task/Enforced-immutability/Zkl/enforced-immutability.zkl
Normal file
5
Task/Enforced-immutability/Zkl/enforced-immutability.zkl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
List(1,2,3).del(0) //--> L(2,3)
|
||||
ROList(1,2,3).del(0) //-->SyntaxError : Can't find del, which means you can't call it
|
||||
d:=Dictionary(); d.add("one",1)
|
||||
D(one:1)
|
||||
d.makeReadOnly(); d.add("2",2) //-->AccessError(This Dictionary is read only)
|
||||
Loading…
Add table
Add a link
Reference in a new issue