Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# forward type declarations

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?

like image 686
Max Avatar asked Sep 04 '09 11:09

Max


3 Answers

You use 'and':

type firstType = 
     | T1 of secondType

and secondType =
     | T1 of firstType
like image 79
Johan Kullbom Avatar answered Sep 20 '22 16:09

Johan Kullbom


I figured it. It's:


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

and secondType =
     | T1 of firstType  
     //................   
like image 23
Max Avatar answered Sep 19 '22 16:09

Max


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

like image 33
t0yv0 Avatar answered Sep 20 '22 16:09

t0yv0