-
Notifications
You must be signed in to change notification settings - Fork 89
Recursive definitions with variant
CodingUnit edited this page Nov 30, 2011
·
2 revisions
-
Category: Defining Types
-
Description: Variants can be defined to give recursive collections of types.
-
Link: see Mutual definitions with records for example of use records as mutual recursive collections
-
Code:
using System;
using System.Console;
// define mutually recursive collections with variants
variant Connection
{
| Airport { Name : string; Connections : list[Connection]; }
| None
// prints contents of class
public override ToString() : string
{
match (this)
{
// if it contains Airport
| Airport(n, c) => $"Airport: Name = $n; Connections = $c"
| _ => "no connection"
}
}
}
module RecursiveDefinition
{
Main() : void
{
def lax = Connection.Airport("Los Angeles Intl Airport", [Connection.None()]);
// using named parameters in contructor
def dtw = Connection.Airport(name = "Detroit Metro Wayne County", connections = [Connection.None()]);
def sea = Connection.Airport("Seattle-Tacoma Intl Airport", connections = [dtw, lax]);
WriteLine(lax);
WriteLine(dtw);
WriteLine(sea);
}
}
- Execution Result:
Airport: Name = Los Angeles Intl Airport; Connections = [no connection]
Airport: Name = Detroit Metro Wayne County; Connections = [no connection]
Airport: Name = Seattle-Tacoma Intl Airport; Connections = [Airport: Name = Detroit Metro Wayne County; Connections = [no connection], Airport: Name = Los Angeles Intl Airport; Connections = [no connection]]
[Copyright ©](Terms of use, legal notice)