简体   繁体   English

F#:如何在参数上声明和使用ByRef语义?

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

I am wraping some SQL Stored Procs with in/out parameters. 我正在使用输入/输出参数包装一些SQL存储过程。 This of course means I have to do ugly stuff like declare my parameters as by reference and use mutables. 这当然意味着我必须做一些丑陋的事情,例如通过引用声明我的参数并使用mutables。

How would I do this in F#? 我怎么在F#中这样做?

F# does indeed have a byref parameter . F#确实有一个byref参数 Here's an example from the MSDN page: 以下是MSDN页面中的示例:

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). 可变变量也存在(尽管使用refmutable变量之间存在重要差异,其中任何一个都可以用于许多相同的目的)。 The MSDN page on this subject is very informative - including a discussion of when to use which keyword/construct. 关于此主题的MSDN页面提供了非常丰富的信息 - 包括讨论何时使用哪个关键字/构造。

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. 如您所见,赋值(以及检索)的语法也在两个构造之间不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM