Change external variables inside a higher order function #24158
Unanswered
gchumillas
asked this question in
Questions and Answers
Replies: 3 comments 7 replies
-
No. As it says in the docs, |
Beta Was this translation helpful? Give feedback.
0 replies
-
Is this a bug? It relly print out |
Beta Was this translation helpful? Give feedback.
5 replies
-
Finally I solved it using a pub struct Ref[T] {
pub mut:
value T
}
pub fn Ref.new[T](value T) &Ref[T] {
return &Ref[T]{ value: value }
}
pub fn my_higher_order_func(f fn ()) {
f()
}
mut x := Ref.new(0)
my_higher_order_func(fn [mut x]() {
x.value = 100
})
println(x) // prints &Ref[int]{ value: 100 } (OK!) |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi! I'd like to modify the variable
x
insidemy_higher_order_func
, and the following script works correctly. However, I found it a bit cumbersome, as I have to create an intermediate pointer variable. Is there an easier solution?Beta Was this translation helpful? Give feedback.
All reactions