3

I have two classes defined like so:

type foo () =
    let getBar() = 
        new bar()

type bar () =
    let getFoo() = 
        new foo()

The compiler is saying

The type 'bar' is not defined

Is there a way to do this?

Jamie Dixon
  • 4,204
  • 4
  • 25
  • 47

1 Answers1

5

this is where the and keyword is used (have to be in the same file obvious):

type Foo () =
   let getBar () =
      Bar ()

and Bar () =
   let getFoo () =
      Foo ()

remark

you can use this too if you have mutual recursive functions (or even values) - but there you have to add the rec keyword to the first binding:

let rec f x = if x > 0 then g x else 0
and g x = f (x-1)
Random Dev
  • 51,810
  • 9
  • 92
  • 119
  • no problem - btw: I did not mention it but you don't have to add `new` in F# when you create objects - indeed it idiomatic to only use `new` if the object implements `IDisposable` (and the compiler will even warn you if you don't use it in this cases) – Random Dev Aug 10 '14 at 18:51
  • PS: and of course types should begin with a captial Letter ;) – Random Dev Aug 10 '14 at 18:52