views:

277

answers:

1

I am wraping some SQL Stored Procs with in/out parameters. This of course means I have to do ugly stuff like declare my parameters as by reference and use mutables.

How would I do this in F#?

+5  A: 

F# does indeed have a byref parameter. Here's an example from the MSDN page:

type Incrementor(z) =
    member this.Increment(i : int byref) =
       i <- i + z

Mutable variables also exist (though there's an important difference between using ref and mutable variables, either of which can be used for many of the same purposes). The MSDN page on this subject is very informative - including a discussion of when to use which keyword/construct.

Example of reference variables:

// Declare a reference.
let refVar = ref 6

// Change the value referred to by the reference.
refVar := 50

Example of mutable variables:

// Declare a reference.
let mutable refVar = 6

// Change the value referred to by the reference.
refVar <- 50

As you can see, the syntax for assignment (as well as retrieval) differs between the two constructs, too.

Noldorin