2009-09-04 10 views
27

Me encontré con este problema en F #. Supongamos, quiero declarar dos tipos que hacen referencia entre sí:F # forward type declarations


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

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

¿Cómo hacer eso, por lo que el compilador no genera un error?

+1

Vea también http://stackoverflow.com/questions/680606/f-how-to-have-two-methods-calling-eachother – Brian

Respuesta

44

Se utiliza 'y':

type firstType = 
    | T1 of secondType 

and secondType = 
    | T1 of firstType 
+2

Cada vez que siento que algo no es elegante en F #, estoy gratamente sorprendido, hay una solución elegante. – CodeMonkey

3

pensé que. Es:


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

and secondType = 
    | T1 of firstType 
    //................ 
+5

también usa la misma notación para funciones recursivas recíprocas, en caso de que no lo supiera. – Massif

2

La limitación es que los tipos deben declararse en el mismo archivo.