Hash :
987f34fb
Author :
Thomas de Grivel
Date :
2024-09-10T23:14:47
Variables in KC3 can be defined using the litteral value for a variable
which is always ?
. You can cast this litteral value and it will not
really be casted but it will give you a typed variable litteral value.
E.g. (List) ?
.
The typed variable litteral value will only accept to be set once to
one value of the variable’s type (in this example the type of a linked
list).
It’s actually a syntax so you cannot rename ?
by mistake and
so is an easy task to do static analysis of variable creation.
The default type for a variable which you can also specify explicitly
is Tag
which is an enum-tagged union type of any other KC3 types
currently defined in the environment. So ?
is exactly equivalent to
(Tag) ?
and they will both accept to be set once to one value of any
type.
A variable is settable once and cannot be changed afterwards (there is an exception if you write C code and link to it but it is not easy nor silent).
This way you do not need to lock or trust any behaviour, once your variable is set to a value the value of the variable will never change, it really is read-only.
You can also use the assignment operator which is <-
which in turn calls
tag_init_copy
. It works like the C assignment operator (=
).
Examples :
# Declare a unsigned byte 8 bits variable "x".
x = (U8) ?
# Set the variable "x" to zero.
x <- 0
# Allocate again for the same binding name "x"
x = (U8) ?
# Also set the new variable "x" to zero with just one Unicode symbol
# that is AltGr+Y on my keyboard.
x ← 0
You can always reset an existing binding at will to another variable litteral and another variable will be created for the same name and it will be in a different memory location, settable once and then read-only again so you can use it without locking.
Previous : 1.5 List
Next : 2 HTTPd
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
# 1.6 Variables
Variables in KC3 can be defined using the litteral value for a variable
which is always `?`. You can cast this litteral value and it will not
really be casted but it will give you a typed variable litteral value.
E.g. `(List) ?`.
The typed variable litteral value will only accept to be set once to
one value of the variable's type (in this example the type of a linked
list).
It's actually a syntax so you cannot rename `?` by mistake and
so is an easy task to do static analysis of variable creation.
The default type for a variable which you can also specify explicitly
is `Tag` which is an enum-tagged union type of any other KC3 types
currently defined in the environment. So `?` is exactly equivalent to
`(Tag) ?` and they will both accept to be set once to one value of any
type.
A variable is settable once and cannot be changed afterwards (there is
an exception if you write C code and link to it but it is not easy nor
silent).
This way you do not need to lock or trust any behaviour, once your
variable is set to a value the value of the variable will never change,
it really is read-only.
You can also use the assignment operator which is `<-` which in turn calls
`tag_init_copy`. It works like the C assignment operator (`=`).
Examples :
```
# Declare a unsigned byte 8 bits variable "x".
x = (U8) ?
# Set the variable "x" to zero.
x <- 0
# Allocate again for the same binding name "x"
x = (U8) ?
# Also set the new variable "x" to zero with just one Unicode symbol
# that is AltGr+Y on my keyboard.
x ← 0
```
## So how do I change anything if it is read-only ?
You can always reset an existing binding at will to another variable
litteral and another variable will be created for the same name and it
will be in a different memory location, settable once and then
read-only again so you can use it without locking.
---
Previous : [1.5 List](1.5_List)
Next : [2 HTTPd](/doc/2_HTTPd)