35

I stumbled across this problem in F#. Suppose, I want to declare two types that reference each other:


type firstType = 
     | T1 of secondType
     //................

type secondType =
     | T1 of firstType  
     //................    

How do I do that, so the compiler does not generate an error?

Brian
  • 117,631
  • 17
  • 236
  • 300
Max
  • 19,654
  • 13
  • 84
  • 122
  • 1
    See also http://stackoverflow.com/questions/680606/f-how-to-have-two-methods-calling-each-other – Brian Sep 04 '09 at 12:23

3 Answers3

55

You use 'and':

type firstType = 
     | T1 of secondType

and secondType =
     | T1 of firstType
Johan Kullbom
  • 4,183
  • 26
  • 28
4

I figured it. It's:


type firstType = 
     | T1 of secondType
     //................

and secondType =
     | T1 of firstType  
     //................   
Max
  • 19,654
  • 13
  • 84
  • 122
  • 6
    you use the same notation for mutually recursive functions as well - in case you didn't already know that. – Massif Sep 04 '09 at 13:17
2

The limitation is that the types have to be declared in the same file.

t0yv0
  • 4,714
  • 19
  • 36