closure tests and fix

This commit is contained in:
sergeych committed 2025-05-20 22:59:15 +04:00
1 parent ebeed385e9
commit 758f49603f
9 files changed
+193 -43

No files matched your search

+50
View File
@@ -0,0 +1,50 @@
# Advanced topics
## Closures/scopes isolation
Each block has own scope, in which it can safely uses closures and override
outer vars:
var param = "global"
val prefix = "param in "
val scope1 = {
var param = prefix + "scope1"
param
}
val scope2 = {
var param = prefix + "scope2"
param
}
// note that block returns its last value
println(scope1)
println(scope2)
println(param)
>>> param in scope1
>>> param in scope2
>>> global
>>> void
One interesting way of using closure isolation is to keep state of the functions:
val getAndIncrement = {
// will be updated by doIt()
var counter = 0
// we return callable fn from the block:
fun doit() {
val was = counter
counter = counter + 1
was
}
}
println(getAndIncrement())
println(getAndIncrement())
println(getAndIncrement())
>>> 0
>>> 1
>>> 2
>>> void
Inner `counter` is not accessible from outside, no way; still it is kept
between calls in the closure, as inner function `doit`, returned from the
block, keeps reference to it and keeps it alive.
+19 -5
View File
@@ -11,10 +11,10 @@ Same as in C++.
| 2 | `+` `-` |
| 3 | bit shifts (NI) |
| 4 | `<=>` (NI) |
| 5 | `<=` `>=` `<` `>` (NI) |
| 6 | `==` `!=` (NI) |
| 7 | `&` (NI) |
| 9 | `\|` (NI) |
| 5 | `<=` `>=` `<` `>` |
| 6 | `==` `!=` |
| 7 | bitwise and `&` (NI) |
| 9 | bitwise or `\|` (NI) |
| 10 | `&&` |
| 11<br/>lowest | `\|\|` |
@@ -22,7 +22,16 @@ Same as in C++.
## Operators
`+ - * / % `: if both operand is `Int`, calculates as int. Otherwise, as real.
`+ - * / % `: if both operand is `Int`, calculates as int. Otherwise, as real:
// integer division:
3 / 2
>>> 1
but:
3 / 2.0
>>> 1.5
## Round and range
@@ -46,6 +55,11 @@ or transformed `Real` otherwise.
| | |
| | |
For example:
sin(π/2)
>>> 1.0
## Scientific constant
| name | meaning |
+14 -2
View File
@@ -154,14 +154,20 @@ Each __block has an isolated context that can be accessed from closures__. For e
}
>>> void
As was told, `def` statement return callable for the function, it could be used as a parameter, or elsewhere
As was told, `fun` statement return callable for the function, it could be used as a parameter, or elsewhere
to call it:
val taskAlias = fun someTask() {
println("Hello")
}
// call the callable stored in the var
taskAlias()
// or directly:
someTask()
>>> Hello
>>> Hello
>>> void
If you need to create _unnamed_ function, use alternative syntax (TBD, like { -> } ?)
# Flow control operators
@@ -276,8 +282,12 @@ We can skip the rest of the loop and restart it, as usual, with `continue` opera
Notice that `total` remains 0 as the end of the outerLoop@ is not reachable: `continue` is always called and always make Ling to skip it.
## Labels@
The label can be any valid identifier, even a keyword, labels exist in their own, isolated world, so no risk of occasional clash. Labels are also scoped to their context and do not exist outside it.
Right now labels are implemented only for the while loop. It is intended to be implemented for all loops and returns.
# Comments
// single line comment
@@ -296,6 +306,8 @@ The label can be any valid identifier, even a keyword, labels exist in their own
| Null | missing value, singleton | null |
| Fn | callable type | |
See also [math operations](math.md)
## String details
### String operations