Hi, the goal is to get a shared variable between functions by defining a closure.
The following minimal working example works as intended:
disp, change = let x = [1,2]
disp = () -> println(x)
change = function()
x .+= 1
end
disp, change
end
change()
disp() # [2,3]
change()
disp() # [3,4]
I would like to have the same behaviour where the change
function isn’t defined in the closure
change = function()
x .+= 1
end
disp, change = let x = [1,2], change=change
disp = () -> println(x)
# somehow bind this x to the x in change
disp, change
end
change()
disp() # [1,2] (wrong)
change()
disp() # [1,2] (wrong)
I’m guessing i’ll need some macro for this.
I tried @inline
in the definition of change, but it didn’t work.
EDIT:
I know this works, but i was wondering if there is another way to do this
change = function(x)
x .+= 1
end
disp, change = let x = [1,2], change=change
disp = () -> println(x)
change_() = change(x)
disp, change_
end
change()
disp() # [2,3]
change()
disp() # [3,4]