MCSN-project2014 / APproject

Project for the MCSN Advanced Programming Module A.Y. 2014/2015
http://mcsn-project2014.github.io/APproject/
MIT License
4 stars 0 forks source link

Mutable variable in anonumous functions #31

Closed dido18 closed 9 years ago

dido18 commented 9 years ago

In FSharp we connot use a mutable variable in a anonymous function The program in FunW@p

fun outside_adder() fun(int) int {
       var sum int = 10
       return fun(x int) int {
            sum += x
           return sum
       }
}

is translated in fsharp:

let outadd = 
    let mutable sum = 10

    fun  x  -> 
        sum <- sum + x        
        sum

But mutable variable sum is used in an invalid way, because variables cannot be captured by closures. there are two possibilities:

teto1992 commented 9 years ago

fixed using ref everywhere and closures