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 (=
).
# 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
x ← 0
Top : KC3 documentation
Previous : 1.23 Tuple
Next : 2 HTTPd