You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While porting some small Rust code to koka, I have been struggling with mutating elements of nested vectors. The koka documentation is really sparse, so I had to browse the standard library source to find a way to do it. After stumbling on the [] function and battling with the compiler, I came up with this code:
fun main()
var v := vector(3, local-new(vector(0, 0)))
for(0, 2) fn(i)
v[i] := local-new(vector(5, 0))
v[2][4] := 666
println(local-get(v[2])[4])
It works fine, even though it's not as concise as the Rust version. But what bugs me is the need to fill the nested vectors in a loop, while also wasting the vector(0, 0)). I wanted to use vector-init
var v := vector-init(3, fn(_) local-new(vector(5, 0)))
but it requires a lambda without effects. Is it possible to modify vector-init so it allows effects in the lambda and propagates them to the caller?
I couldn't find any docs on local-var and how am I supposed to use it correctly. In the recent discussion on Gitter, someone suggested that a local-var should not escape its lexical scope. This begs the question: what is considered a lexical scope? Is the lexical scope of local-new(vector(5, 0)) the for lambda or the whole function? Is my code correct or is there some compiler bug that I'm unknowingly exploiting here?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
While porting some small Rust code to
koka
, I have been struggling with mutating elements of nested vectors. The koka documentation is really sparse, so I had to browse the standard library source to find a way to do it. After stumbling on the[]
function and battling with the compiler, I came up with this code:It works fine, even though it's not as concise as the Rust version. But what bugs me is the need to fill the nested vectors in a loop, while also wasting the
vector(0, 0))
. I wanted to usevector-init
but it requires a lambda without effects. Is it possible to modify
vector-init
so it allows effects in the lambda and propagates them to the caller?I couldn't find any docs on
local-var
and how am I supposed to use it correctly. In the recent discussion on Gitter, someone suggested that alocal-var
should not escape its lexical scope. This begs the question: what is considered a lexical scope? Is the lexical scope of local-new(vector(5, 0)) thefor
lambda or the whole function? Is my code correct or is there some compiler bug that I'm unknowingly exploiting here?Beta Was this translation helpful? Give feedback.
All reactions