10

Is it possible to have mutual recursive types ([<Struct>]) spread across different files? The types are directly under a namespace.

My solution is to put them in one big file and use type ... and ... and ... etc construction. Is it the only way?

elmattic
  • 12,046
  • 5
  • 43
  • 79

1 Answers1

18

You can use a technique called untying the recursive knot where you parameterize one over the other.

So this:

type a = B of b
and b = A of a

becomes:

type 'b a = B of 'b
type b = A of b a
J D
  • 48,105
  • 13
  • 171
  • 274
  • Thanks for the answer. Is this kind of technique often used? I feel that F# design guidelines recommand to put all in one big file, right? – elmattic Dec 05 '10 at 22:12
  • I certainly use it often and recommend to our clients that they use it often but, yes, it does seem to be underappreciated. – J D Dec 06 '10 at 09:18
  • 1
    `untying the recursive knot` is very useful when you try to memoize functions. It has been described in the *When we need substitution* of http://typeocaml.com/2015/01/20/mutable/ – Jackson Tale Jan 22 '15 at 10:26