F#: How do I declare and use ByRef semantics on parameters?

时间秒杀一切 提交于 2019-12-10 22:28:24

问题


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#?


回答1:


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.



来源:https://stackoverflow.com/questions/946338/f-how-do-i-declare-and-use-byref-semantics-on-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!