byref parameters in F#

Some Times we need to change Parameters of Functions like interoperating with other .NET Languages. If you want to achieve this in F# You need to use a combination of values, the byref keyword and Address-of operator(&). Like Below Example.

Example

let incrementParam(a: int byref) = a <- a + 1
let mutable b = 30
incrementParam(&b)

Above example showing that incrementparam's argument a is of type int byref. The byref keyword tells the compiler to expect the address of a mutable value. In the next line we use the mutable keyword so that when we pass b to incrementparam. The function can successfully change b's value.

Lastly when we call incrementparam, we provide the address of the mutable values and we do this by using the address-of operator(&). byref parameter works with value type like premitive.

Ebook Download
View all
Learn
View all