为什么F#引号不能包含struct?
我想定义一个结构,它包含一些成员方法。我想使用为什么F#引号不能包含struct?,f#,F#,我想定义一个结构,它包含一些成员方法。我想使用[]标记该结构成员方法。但是编译器告诉我这是错误的 首先,请看这段代码: 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
[]
标记该结构成员方法。但是编译器告诉我这是错误的
首先,请看这段代码:
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是value类型,所以这个指针应该是byref。事实上,在F#3.0中观察到的编译器错误如下: 错误FS1220:ReflectedDefinitionAttribute不能应用于实例 结构类型上的成员,因为实例成员采用隐式“this” byref参数 观察到的行为的根本原因是F#引号的限制-您不能在引用的代码中使用
byref
类型
然而,此限制的结果仅适用于实例
struct
成员<代码>静态结构
成员可以用[
属性修饰。@JohnPalmer感谢您的语法编辑:)
[<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