Discriminated Unions

The input file in this example Library.fs looks like this:

namespace Example
open Myriad.Plugins

[<Generator.DuCases "dus" >]
type Currency =
| CAD
| PLN
| EUR
| USD
| Custom of string

An attribute is used by the plugin so that the code generator plugin knows which parts of the input AST are to be used by the plugin. If you had several records and you only wanted the fields plugin to operate on Currency then the attribute would be used like in the example about to only apply Generator.DuCases to Currency. Note, if you wanted a plugin that just needs the whole input AST then there is no need to provide an input. Myriad aims to be a library rather than a full framework that ties you to the mechanism used to input and generate code.

To control what namespace is used as well as supplying any other information to plugins a myriad.toml configuration file is used. This is specified in the generator attribute, in the example above you can see the configuration section is dus [<Generator.DuCases "dus" >]. The corresponding myriad.toml file looks as follows:

[dus]
namespace = "TestDus"

The DuCases plugin uses only a single configuration key namespace to control the resulting namespace the DuCases are generated

The fields plugin in this example will generate the following code at prebuild time and compile the code into your assembly:

//------------------------------------------------------------------------------
//        This code was generated by myriad.
//        Changes to this file will be lost when the code is regenerated.
//------------------------------------------------------------------------------
namespace rec TestDus

module Currency =
    open Input

    let toString (x: Currency) =
        match x with
        | CAD -> "CAD"
        | PLN -> "PLN"
        | EUR -> "EUR"
        | USD -> "USD"
        | Custom _ -> "Custom"

    let fromString (x: string) =
        match x with
        | "CAD" -> Some CAD
        | "PLN" -> Some PLN
        | "EUR" -> Some EUR
        | "USD" -> Some USD
        | _ -> None

    let toTag (x: Currency) =
        match x with
        | CAD -> 0
        | PLN -> 1
        | EUR -> 2
        | USD -> 3
        | Custom _ -> 4

    let isCAD (x: Currency) =
        match x with
        | CAD -> true
        | _ -> false

    let isPLN (x: Currency) =
        match x with
        | PLN -> true
        | _ -> false

    let isEUR (x: Currency) =
        match x with
        | EUR -> true
        | _ -> false

    let isUSD (x: Currency) =
        match x with
        | USD -> true
        | _ -> false

    let isCustom (x: Currency) =
        match x with
        | Custom _ -> true
        | _ -> false