Scala: How to make a closure not to see changes in its free variable? -


in following code snippet, closure foo see changes made in x should in scala. however, how can make local variable y in foo hold value of x permanently , not see changes?

scala> var x = 10 x: int = 10  scala> val foo = (a:int) => {val y = x; + y} foo: int => int = <function1>  scala> foo(3) res1: int = 13  scala> x = 5 x: int = 5  scala> foo(3)    //see changes made in x. how can make closure not see changes made on x? res2: int = 8 

you this:

val foo = ((x:int) => (a:int) => {val y = x; + y})(x) 

in case x bound in foo.

what doing example of closure.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -