V/var keyword
The
V/
varstatement declares a variable initializing it to a value.
V variable_name = value
You can declare more than one variable at a time with a tuple assignment:
F get_row_column() // this is a function returning a tuple ... R (row, col) ... V (row, col) = get_row_column() // analogous code: `V rc = get_row_column(); V row = rc[0]; V col = rc[1]`
Partial declaration is also possible:
(V row2, col) = get_row_column() // `col` has already been declared, `row2` is declared hereor:
(row, V col2) = get_row_column() // `row` has already been declared, `col2` is declared here
To declare and initialize to some value multiple variables at once use this notation ({…}):
V a = V b = 0
To declare a constant "variable" use
-V/
-var:
-V constant_name = value[{…}]
To reduce the amount of
V/
varkeywords you can use a new scope. For example, this code:
-V n_rows = 10 n_cols = 10 grid_size = n_rows * n_cols min_words = 25is equivalent to this:
-V n_rows = 10 -V n_cols = 10 -V grid_size = n_rows * n_cols -V min_words = 25
V?
and V&
V? i = ‘str’.find(‘s’) F print_set(DefaultDict[Int, Set[Int]] ↦ key) V& s = map[key] // `V s = map[key]` is forbidden (but you can write `V s = copy(map[key])`)
I V
I (V&? sheep = Sheep?(animal)) != N print(sheep.some_sheep_prop)can be shortened {…} to:
I V sheep = Sheep?(animal) ...because there is no hidden copying and sharing in 11l.
And like in Swift {…}
I (V sheep = Sheep?(animal)) {...}is forbidden.
However, in 11l this is also possible:
I T(animal) == Sheep // or `I T(animal) >= Sheep` print(animal.some_sheep_prop)
Also note this:
I V v = ... // `v` is like a constant reference, so v = ... // this is forbidden // And if you want `v = ...` then: I V& v = ... // write this I V v = copy(...) // or this[This may seems strange, but there is a similar behaviour in other parts of 11l:
L(el)vs
L(&el), and function arguments (which are immutable ‘by default’/‘without qualifiers’).
However, despite of
v = ...is forbidden, you can call mutable methods of
v(for the sake of convenience).]
V E
Instead of:
V&? el = map.get(key) I el == N print(‘Key <’key‘> is not found!’) Rit is better to write:
V& el = map.get(key) E print(‘Key <’key‘> is not found!’) RThis syntax is inspired by
guard let{…} in Swift, but it is also a logical enough, because
V& el = map.get(key)!is the same as:
V& el = map.get(key) E X.throw NullPointerException()