Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# how to deal with this recursive mutal class method

Tags:

f#

I have the following piece of code where I need to call on my own class that I reference via a reference on a mutual class(hope that makes sense, if not the code sample explains it better).

Unfortunately it does work because I get the "Lookup on object of indeterminate type prior to..." error on the 'x.Connections.[0].Dest.Func' part.

Can anyone think of a way to fix this, possibly I will have to just go for quite a different approach?

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

Ignore the fact that calling the Func method on Node serves no purpose, I've simplified this from the real code to try to just show what causes the problem. Thanks in advance.

like image 909
Daniel Slater Avatar asked Jan 23 '26 12:01

Daniel Slater


1 Answers

You can also just add a type annotation to Dest, which appears to be the critical symbol that throws the type inference algorithm into a loop:

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
like image 139
piaste Avatar answered Jan 25 '26 12:01

piaste