F#类成员中的ref和byref <>参数

时间:2019-09-19 11:59:52

标签: f#

我将一些f#函数定义重写为静态成员,并停留在ref / byref参数错误处:

static member bar (a : byref<int>) = Foo.bar &a 

Error   FS0001  This expression was expected to have type
    'int ref'    
but here has type
    'byref<'a>' 

“ let”和静态定义之间的byref参数是否存在某些差异?

UPD:

这是我更改为静态成员定义的有效示例:

> let rec foo (a :byref<int>) = 
    a <- a-1
    if a > 0 then
        System.Console.Write(a.ToString()); foo &a
    else a
;;
val foo : a:byref<int> -> int

> let mutable a = 3;; 
val mutable a : int = 3

> foo &a;;
21val it : int = 0

现在看起来像这样,它不起作用。为什么? :

> type Foo() =
    static member bar (a : byref<int>) = 
        a <- a-1
        if a > 0 then
            System.Console.Write(a.ToString()); foo &a
        else a;;
type Foo =
  class
    new : unit -> Foo
    static member bar : a:byref<int> -> int
  end   

> let mutable b = 3;;
val mutable b : int = 3

> Foo.bar &b;;

  Foo.bar &b;;
  --------^^

stdin(71,9): error FS0001: This expression was expected to have type
    'int ref'    
but here has type
    'byref<'a>'    

1 个答案:

答案 0 :(得分:2)

{data: data}是F#Reference Cell的类型定义,而int ref等效于C#byref<'a> / ref自变量(即使这是一个值类型。

虽然没有可重复的代码段是不可能说出更多信息的,但您很有可能将两种类型混为一谈

out