F#如何处理这种递归的多类方法

F#如何处理这种递归的多类方法,f#,F#,我有下面的一段代码,我需要调用我自己的类,我通过对一个共同类的引用来引用它(希望这是有意义的,如果不是的话,代码示例可以更好地解释它) 不幸的是,它确实起作用了,因为我在“x.Connections.[0].Dest.Func”部分上遇到了“查找不确定类型的对象之前…”错误 有人能想出一个办法来解决这个问题吗?我可能不得不采用完全不同的方法 type Node() = member x.Connections = new System.Collections.Generic.List&l

我有下面的一段代码,我需要调用我自己的类,我通过对一个共同类的引用来引用它(希望这是有意义的,如果不是的话,代码示例可以更好地解释它)

不幸的是,它确实起作用了,因为我在“x.Connections.[0].Dest.Func”部分上遇到了“查找不确定类型的对象之前…”错误

有人能想出一个办法来解决这个问题吗?我可能不得不采用完全不同的方法

type Node() =
    member x.Connections = new System.Collections.Generic.List<Connection>()
    member x.Connect other = x.Connections.Add(Connection(x, other))
    member x.Func i : unit = x.Connections.[0].Dest.Func i
and Connection(source : Node, dest : Node) =
    member val Source = source
    member val Dest = dest
类型节点()=
成员x.Connections=new System.Collections.Generic.List()
成员x.Connect other=x.Connections.Add(连接(x,other))
成员x.Func i:unit=x.Connections[0].Dest.Func i
和连接(源:节点,目标:节点)=
成员val源=源
成员val Dest=Dest
忽略在节点上调用Func方法没有任何作用这一事实,我已经从实际代码中简化了这一点,试图说明问题的原因。
提前感谢。

最后尝试声明该方法:

type Node() =
    member x.Connections = new System.Collections.Generic.List<Connection>()
    member x.Connect other = x.Connections.Add(Connection(x, other))

and Connection(source : Node, dest : Node) =
    member val Source = source
    member val Dest = dest

type Node with
    member x.Func i : unit = x.Connections.[0].Dest.Func i
类型节点()=
成员x.Connections=new System.Collections.Generic.List()
成员x.Connect other=x.Connections.Add(连接(x,other))
和连接(源:节点,目标:节点)=
成员val源=源
成员val Dest=Dest
使用
成员x.Func i:unit=x.Connections[0].Dest.Func i
或者最好交换类型的顺序:

type Connection(source : Node, dest : Node) =
    member val Source = source
    member val Dest = dest
and Node() =
    member x.Connections = new System.Collections.Generic.List<Connection>()
    member x.Connect other = x.Connections.Add(Connection(x, other))
    member x.Func i : unit = x.Connections.[0].Dest.Func i
类型连接(源:节点,目标:节点)=
成员val源=源
成员val Dest=Dest
和节点()=
成员x.Connections=new System.Collections.Generic.List()
成员x.Connect other=x.Connections.Add(连接(x,other))
成员x.Func i:unit=x.Connections[0].Dest.Func i

您也可以只向
Dest
添加一个类型注释,它似乎是将类型推断算法抛出循环的关键符号:

type Node() =
    member x.Connections = new System.Collections.Generic.List<Connection>()
    member x.Connect other = x.Connections.Add(Connection(x, other))    
    member x.Func i : unit = x.Connections.[0].Dest.Func i                             
and Connection(source : Node, dest : Node) =
    member val Source = source
    member val Dest : Node = dest
类型节点()=
成员x.Connections=new System.Collections.Generic.List()
成员x.Connect other=x.Connections.Add(连接(x,other))
成员x.Func i:unit=x.Connections[0].Dest.Func i
和连接(源:节点,目标:节点)=
成员val源=源
成员val Dest:Node=Dest

非常感谢,有两个很棒的解决方案。第二种方法对我的现实世界并不适用,但第一种方法很完美。是的,这只适用于这段代码,这就是为什么我将其作为第二种方法编写的原因。是的,这实际上也适用。我很惊讶编译器需要提示,因为它应该能够很容易地推断出来,它看起来就像编译器中的一个bug。。。