F#:如何在参数上声明和使用ByRef语义? [英] F#: How do I declare and use ByRef semantics on parameters?

查看:51
本文介绍了F#:如何在参数上声明和使用ByRef语义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在包装一些带有in / out参数的SQL存储过程。当然,这意味着我必须做一些丑陋的事情,例如通过引用声明我的参数并使用可变变量。

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.

我将如何在F#中执行此操作?

How would I do this in F#?

推荐答案

F#可以确实有 byref 参数。这是MSDN页面上的示例:

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

还存在可变变量(尽管存在使用 ref mutable 变量之间的重要区别,这两个变量均可用于许多相同的目的)。关于该主题的 MSDN页面内容非常丰富-包括何时使用哪个关键字/构造的讨论。

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.

参考变量示例:

// Declare a reference.
let refVar = ref 6

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

可变变量的示例:

// 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.

这篇关于F#:如何在参数上声明和使用ByRef语义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆