如何在F#中拥有两种相互递归类型



以下代码不编译:

[<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

问题位于[<Struct>]属性上,似乎与"one_answers"构造相撞。

我应该怎么做?我知道我可以用

来完成任务
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

,但我喜欢[<Struct>]简洁。

谢谢

and令牌之后移动属性定义

and [<Struct>] Edge(target:Point, cost:int) =

详细说明贾里德的答案:

per f#spec(8.键入定义)自定义属性可以立即放置在类型定义组之前,在这种情况下,它们适用于第一个类型定义,或者在类型定义的名称

的名称之前

意味着您也可以使用此样式:

type
    [<Struct>]
    A(x : int) = 
        member this.X = x
and
    [<Struct>]
    B(y : int) = 
        member this.Y = y 

最新更新