Organise my mutual recursive types
Asked Answered
D

1

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?

Destroy answered 29/11, 2010 at 21:38 Comment(0)
M
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
Mug answered 29/11, 2010 at 22:28 Comment(3)
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?Destroy
I certainly use it often and recommend to our clients that they use it often but, yes, it does seem to be underappreciated.Mug
untying the recursive knot is very useful when you try to memoize functions. It has been described in the When we need substitution of typeocaml.com/2015/01/20/mutableMozellemozes

© 2022 - 2024 — McMap. All rights reserved.