It is in fact quite easy to expose the fact that there are several variables "bar" here, shadowing one another, if you close over them with a lambda:

> let x = 1;;

val x : int = 1

> let f1 = fun() -> x;;

val f1 : unit -> int

> let x = 2;;

val x : int = 2

> let f2 = fun() -> x;;

val f2 : unit -> int

> f1();;

val it : int = 1

> f2();;

val it : int = 2

>

By on 11/17/2009 1:29 PM ()

Unlike some languages, F# supports and encourages shadowing. So when you do "let a = 1; let a = a + 2", you're simply "re-binding" the name a to a new value. The definition of the second binding can use the first binding because the second binding isn't defined yet.

In fact, the types don't even need to match:

1
2
3
4
5
6
7
8
 

> let a = 1;;
val a : int = 1

> let a = string a + " oh";;
val a : string = "1 oh"

The second a is completely independent from the original a. The only link is that the second a happened to use the first a's value.

By on 11/17/2009 11:23 AM ()

However, it won't allow me to do this at

the module level. For example:

let f = 1

let f = 3

The VS editor will flag a "duplicate defintion of value f" warning.

By on 11/17/2009 11:43 AM ()

Right because it's exporting those values from the module. In a class constructor, you're shadowing them and they are not exported directly. But you can do this in a module:

1
2
3
4
5
 
let f = 
   let f = 1
   let f = f + 3
   f
By on 11/17/2009 11:49 AM ()

Also, direct mutability will happen with <-

1
2
3
 
let mutable a = 1
a <- a + 1

In that case, we're directly updating the value of binding a; just like = does in C style languages.

By on 11/17/2009 11:25 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper