我想定义一个结构,其中包含一些成员方法。我想用它[<ReflectedDefinition>]
来标记那个结构成员方法。但是编译器告诉我这是错误的。
首先,看这段代码:
type Int4 =
val mutable x : int
val mutable y : int
val mutable z : int
val mutable w : int
[<ReflectedDefinition>]
new (x, y, z, w) = { x = x; y = y; z = z; w = w }
[<ReflectedDefinition>]
member this.Add(a:int) =
this.x <- this.x + a
this.y <- this.y + a
this.z <- this.z + a
this.w <- this.w + a
override this.ToString() = sprintf "(%d,%d,%d,%d)" this.x this.y this.z this.w
它编译。但是,如果我将类型设为结构,则无法编译:
[<Struct>]
type Int4 =
val mutable x : int
val mutable y : int
val mutable z : int
val mutable w : int
[<ReflectedDefinition>]
new (x, y, z, w) = { x = x; y = y; z = z; w = w }
[<ReflectedDefinition>]
member this.Add(a:int) = // <----------- here the 'this' report compile error
this.x <- this.x + a
this.y <- this.y + a
this.z <- this.z + a
this.w <- this.w + a
override this.ToString() = sprintf "(%d,%d,%d,%d)" this.x this.y this.z this.w
我收到以下错误:
error FS0462: Quotations cannot contain this kind of type
这是此代码中的指向this
。
任何人都知道为什么我不能在结构成员函数上创建引号?我想这可能是因为该结构是值类型,所以这个指针应该是 byref。