This project is read-only.
Mutual Definitions

Category:
Defining Types
Description: Discriminated unions and records can be defined simultaneously to give mutually recursive collections of types.
Code:
// define mutually recursive collections with the 'and' keyword
type Airport =
{ Name: string;
Connections: connection list }
and connection =
| Connection of Airport
| None

let MutualDefinition() =
let LAX = { Name = "Los Angeles Intl Airport"; Connections = [connection.None] }
let DTW = { Name = "Detroit Metro Wayne County"; Connections = [connection.None] }
let SEA = { Name = "Seattle-Tacoma Intl Airport";
Connections = [connection.Connection DTW; connection.Connection LAX] }

printfn "%A" LAX
printfn "\n%A" DTW
printfn "\n%A" SEA

Execution Result:
{Name = "Los Angeles Intl Airport";
Connections = [None];}

{Name = "Detroit Metro Wayne County";
Connections = [None];}

{Name = "Seattle-Tacoma Intl Airport";
Connections =
[Connection {Name = "Detroit Metro Wayne County";
Connections = [None];};
Connection {Name = "Los Angeles Intl Airport";
Connections = [None];}];}

Last edited Sep 14, 2011 at 3:31 AM by ttliu2000, version 1

Comments

No comments yet.