views:

337

answers:

2

I'm a bit confused as to how to get two method to call each other (i.e., have A() call B() and B() call A()). It seems that F# only 'sees' the method after it's been encountered in code, so if it hasn't, it just says value or constructor has not been defined.

Am I missing something very basic here?

+13  A: 

'let rec... and...' is the syntax you seek.

let rec F() = 
    G()
and G() =
    F()

See also http://blogs.msdn.com/jomo_fisher/archive/2007/09/24/adventures-in-f-corecursion.aspx

Brian
+6  A: 

Since the question is about methods, and Brian's answer is about functions, maybe it's useful to point out that you can use a similar syntax for types:

type A() =
    let b = new B()
    member x.MethodA() = b.MethodB()
and B() =
    member x.MethodB() = ()

Note also that members are 'let rec' by default (in fact I don't think they can be not recursive).

Kurt Schelfthout