以下代码无法编译:
[<Struct>]
type Point(x:int, y:int) =
member __.X = x
member __.Y = y
member __.Edges = ArrayList<Edge>()
[<Struct>]
and Edge(target:Point, cost:int) =
member __.Target = target
member __.Cost = cost
Run Code Online (Sandbox Code Playgroud)
问题在于[<Struct>]属性,它们似乎与"和"构造冲突.
我应该怎么做呢?我知道我可以选择完成任务
type Point(x:int, y:int) =
struct
member __.X = x
member __.Y = y
member __.Edges = new ArrayList<Edge>()
end
and Edge(target:Point, cost:int) =
struct
member __.Target = target
member __.Cost = cost
end
Run Code Online (Sandbox Code Playgroud)
但我喜欢[<Struct>]简洁.
谢谢
在and令牌之后移动属性定义
and [<Struct>] Edge(target:Point, cost:int) =
Run Code Online (Sandbox Code Playgroud)
详细说明Jared的答案:
每个F#规范(8.类型定义) 自定义属性可以紧接在类型定义组之前,在这种情况下,它们适用于第一个类型定义,或紧接在类型定义的名称之前
意思是你也可以使用这种风格:
type
[<Struct>]
A(x : int) =
member this.X = x
and
[<Struct>]
B(y : int) =
member this.Y = y
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
215 次 |
| 最近记录: |