Discriminated Unions
Classes are types that represent objects that can have properties, methods, and events. Discriminated unions provide support for values that can be one of a number of named cases, possibly each with different values and types. Discriminated unions are useful for heterogeneous data; data that can have special cases, including valid and error cases; data that varies in type from one instance to another; and as an alternative for small object hierarchies. In addition, recursive discriminated unions are used to represent tree data structures.
For details on how the AST node works, please refer to the Fantomas Core documentation. See also official documentation for a comparison between the two.
Constructors
Constructors |
Description |
---|---|
Union(name: string) |
Creates a TypeDefnUnionNode AST node with the specified name. |
Modifiers
Modifier |
Description |
---|---|
members() |
Adds members to the type definition. |
typeParams(typeParams: WidgetBuilder |
Adds type parameters to the type definition. |
xmlDocs(xmlDocs: string list) |
Adds XML documentation comments to the type definition. |
attributes(attributes: WidgetBuilder |
Adds multiple attributes to the type definition. |
attribute(attribute: WidgetBuilder |
Adds a single attribute to the type definition. |
toRecursive() |
Adds a scalar indicating that the type definition is recursive. |
toPrivate() |
Sets the accessibility to the type definition to private. |
toPublic() |
Sets the accessibility of the type definition to public. |
toInternal() |
Sets the accessibility of the type definition to internal. |
xmlDocs(xmlDocs: string list) |
Adds XML documentation comments to the type definition. |
#r "../../src/Fabulous.AST/bin/Release/netstandard2.1/publish/Fantomas.Core.dll"
#r "../../src/Fabulous.AST/bin/Release/netstandard2.1/publish/Fabulous.AST.dll"
#r "../../src/Fabulous.AST/bin/Release/netstandard2.1/publish/Fantomas.FCS.dll"
open Fabulous.AST
open type Fabulous.AST.Ast
Oak() {
AnonymousModule() {
Union("Option") {
UnionCase("Some", "'a")
UnionCase("None")
}
|> _.typeParams(PostfixList("'a"))
Union("Shape") {
UnionCase("Line")
UnionCase("Rectangle", [ Field("width", Float()); Field("length", Float()) ])
UnionCase("Circle", Field("radius", Float()))
UnionCase("Prism", [ Field("width", Float()); Field("float"); Field("height", Float()) ])
}
}
}
|> Gen.mkOak
|> Gen.run
|> printfn "%s"
// produces the following code:
|
static member Ast.Oak: unit -> CollectionBuilder<Fantomas.Core.SyntaxOak.Oak,'marker>
--------------------
module Oak from Fabulous.AST
static member Ast.Union: name: string -> CollectionBuilder<Fantomas.Core.SyntaxOak.TypeDefnUnionNode,Fantomas.Core.SyntaxOak.UnionCaseNode>
--------------------
module Union from Fabulous.AST
static member Ast.UnionCase: name: string * fields: (string * WidgetBuilder<Fantomas.Core.SyntaxOak.Type>) list -> WidgetBuilder<Fantomas.Core.SyntaxOak.UnionCaseNode>
static member Ast.UnionCase: name: string * fields: (string * string) list -> WidgetBuilder<Fantomas.Core.SyntaxOak.UnionCaseNode>
static member Ast.UnionCase: name: string * field: string -> WidgetBuilder<Fantomas.Core.SyntaxOak.UnionCaseNode>
static member Ast.UnionCase: name: string * field: WidgetBuilder<Fantomas.Core.SyntaxOak.FieldNode> -> WidgetBuilder<Fantomas.Core.SyntaxOak.UnionCaseNode>
static member Ast.UnionCase: name: string * fields: string list -> WidgetBuilder<Fantomas.Core.SyntaxOak.UnionCaseNode>
static member Ast.UnionCase: name: string * field: WidgetBuilder<Fantomas.Core.SyntaxOak.Type> -> WidgetBuilder<Fantomas.Core.SyntaxOak.UnionCaseNode>
static member Ast.UnionCase: name: string * fields: WidgetBuilder<Fantomas.Core.SyntaxOak.Type> list -> WidgetBuilder<Fantomas.Core.SyntaxOak.UnionCaseNode>
static member Ast.UnionCase: name: string * fields: WidgetBuilder<Fantomas.Core.SyntaxOak.FieldNode> list -> WidgetBuilder<Fantomas.Core.SyntaxOak.UnionCaseNode>
static member Ast.UnionCase: name: string -> WidgetBuilder<Fantomas.Core.SyntaxOak.UnionCaseNode>
--------------------
module UnionCase from Fabulous.AST
static member Ast.PostfixList: decls: WidgetBuilder<Fantomas.Core.SyntaxOak.TyparDeclNode> * constraints: WidgetBuilder<Fantomas.Core.SyntaxOak.TypeConstraint> -> WidgetBuilder<Fantomas.Core.SyntaxOak.TyparDecls>
static member Ast.PostfixList: constraints: WidgetBuilder<Fantomas.Core.SyntaxOak.TypeConstraint> -> WidgetBuilder<Fantomas.Core.SyntaxOak.TyparDecls>
static member Ast.PostfixList: constraints: WidgetBuilder<Fantomas.Core.SyntaxOak.TypeConstraint> list -> WidgetBuilder<Fantomas.Core.SyntaxOak.TyparDecls>
static member Ast.PostfixList: decl: string -> WidgetBuilder<Fantomas.Core.SyntaxOak.TyparDecls>
static member Ast.PostfixList: decl: WidgetBuilder<Fantomas.Core.SyntaxOak.TyparDeclNode> -> WidgetBuilder<Fantomas.Core.SyntaxOak.TyparDecls>
static member Ast.PostfixList: decls: string list -> WidgetBuilder<Fantomas.Core.SyntaxOak.TyparDecls>
static member Ast.PostfixList: decls: WidgetBuilder<Fantomas.Core.SyntaxOak.TyparDeclNode> list -> WidgetBuilder<Fantomas.Core.SyntaxOak.TyparDecls>
static member Ast.PostfixList: decls: string list * constraints: WidgetBuilder<Fantomas.Core.SyntaxOak.TypeConstraint> list -> WidgetBuilder<Fantomas.Core.SyntaxOak.TyparDecls>
static member Ast.PostfixList: decls: WidgetBuilder<Fantomas.Core.SyntaxOak.TyparDeclNode> list * constraints: WidgetBuilder<Fantomas.Core.SyntaxOak.TypeConstraint> list -> WidgetBuilder<Fantomas.Core.SyntaxOak.TyparDecls>
static member Ast.Field: name: string * fieldType: string -> WidgetBuilder<Fantomas.Core.SyntaxOak.FieldNode>
static member Ast.Field: name: string * fieldType: WidgetBuilder<Fantomas.Core.SyntaxOak.Type> -> WidgetBuilder<Fantomas.Core.SyntaxOak.FieldNode>
static member Ast.Field: fieldType: string -> WidgetBuilder<Fantomas.Core.SyntaxOak.FieldNode>
static member Ast.Field: fieldType: WidgetBuilder<Fantomas.Core.SyntaxOak.Type> -> WidgetBuilder<Fantomas.Core.SyntaxOak.FieldNode>
--------------------
module Field from Fabulous.AST
static member Ast.Float: unit -> WidgetBuilder<Fantomas.Core.SyntaxOak.Type>
<summary> It takes the root of the widget tree and create the corresponding Fantomas node, and recursively creating all children nodes </summary>