gleamgen/type_/custom

Types

pub opaque type CustomType(repr, generics)

Define a custom type. Once you have built the type, add it to the module with module.with_custom_type. If you want to add type safety to an imported type, use module.with_imported_custom_type.

let animals =
  custom.new()
  |> custom.with_variant(fn(_) {
    variant.new("Dog")
    |> variant.with_argument(option.Some("bones"), type_.int)
  })
  |> custom.with_variant(fn(_) {
    variant.new("Cat")
    |> variant.with_argument(option.Some("name"), type_.string)
    |> variant.with_argument(option.Some("has_catnip"), type_.bool)
  })
use animal_type, dog_constructor, cat_constructor <- module.with_custom_type2(
  definition.new("Animal") |> definition.with_publicity(True),
  animals,
)

To create, see new, with_generic, and with_variant.

pub type CustomTypeBuilder(repr, variants, generics) {
  CustomTypeBuilder(
    variants: List(variant.Variant(type_.Dynamic)),
    generics_list: List(String),
    generics: generics,
  )
}

Constructors

Represents the generics of a custom type with 1 generic parameter.

pub type Generics1(a) =
  #(#(), a)

Represents the generics of a custom type with 2 generic parameters.

pub type Generics2(a, b) =
  #(#(#(), a), b)

Represents the generics of a custom type with 3 generic parameters.

pub type Generics3(a, b, c) =
  #(#(#(#(), a), b), c)

Represents the generics of a custom type with 4 generic parameters.

pub type Generics4(a, b, c, d) =
  #(#(#(#(#(), a), b), c), d)

Represents the generics of a custom type with 5 generic parameters.

pub type Generics5(a, b, c, d, e) =
  #(#(#(#(#(#(), a), b), c), d), e)

Represents the generics of a custom type with 6 generic parameters.

pub type Generics6(a, b, c, d, e, f) =
  #(#(#(#(#(#(#(), a), b), c), d), e), f)

Represents the generics of a custom type with 7 generic parameters.

pub type Generics7(a, b, c, d, e, f, g) =
  #(#(#(#(#(#(#(#(), a), b), c), d), e), f), g)

Represents the generics of a custom type with 8 generic parameters.

pub type Generics8(a, b, c, d, e, f, g, h) =
  #(#(#(#(#(#(#(#(#(), a), b), c), d), e), f), g), h)

Represents the generics of a custom type with 9 generic parameters.

pub type Generics9(a, b, c, d, e, f, g, h, i) =
  #(#(#(#(#(#(#(#(#(#(), a), b), c), d), e), f), g), h), i)

Values

pub fn new() -> CustomTypeBuilder(typed_representation, #(), #())

Create a new CustomTypeBuilder. See also new_dynamic for creating a custom type from a dynamic list of variants and generics.

pub fn new_dynamic(
  variants: List(variant.Variant(type_.Dynamic)),
  generics_list: List(String),
) -> CustomTypeBuilder(
  repr,
  typed_representation,
  List(type_.GeneratedType(type_.Dynamic)),
)
pub fn render(
  type_: CustomTypeBuilder(repr, variants, generics),
  context: render.Context,
) -> render.Rendered
pub fn to_dynamic(
  old: CustomTypeBuilder(repr, variants, generics),
) -> CustomTypeBuilder(type_.Dynamic, Nil, Nil)
pub fn to_type(
  input: CustomType(repr, #()),
) -> type_.GeneratedType(CustomType(repr, #()))
pub fn to_type1(
  input: CustomType(repr, #(#(), type_.GeneratedType(a))),
  type1: type_.GeneratedType(type_a),
) -> type_.GeneratedType(
  CustomType(repr, #(#(), type_.GeneratedType(type_a))),
)
pub fn to_type2(
  input: CustomType(
    repr,
    #(#(#(), type_.GeneratedType(a)), type_.GeneratedType(b)),
  ),
  type1: type_.GeneratedType(type_a),
  type2: type_.GeneratedType(type_b),
) -> type_.GeneratedType(
  CustomType(
    repr,
    #(
      #(#(), type_.GeneratedType(type_a)),
      type_.GeneratedType(type_b),
    ),
  ),
)
pub fn to_type3(
  input: CustomType(
    repr,
    #(
      #(#(#(), type_.GeneratedType(a)), type_.GeneratedType(b)),
      type_.GeneratedType(c),
    ),
  ),
  type1: type_.GeneratedType(type_a),
  type2: type_.GeneratedType(type_b),
  type3: type_.GeneratedType(type_c),
) -> type_.GeneratedType(
  CustomType(
    repr,
    #(
      #(
        #(#(), type_.GeneratedType(type_a)),
        type_.GeneratedType(type_b),
      ),
      type_.GeneratedType(type_c),
    ),
  ),
)
pub fn to_type4(
  input: CustomType(
    repr,
    #(
      #(
        #(#(#(), type_.GeneratedType(a)), type_.GeneratedType(b)),
        type_.GeneratedType(c),
      ),
      type_.GeneratedType(d),
    ),
  ),
  type1: type_.GeneratedType(type_a),
  type2: type_.GeneratedType(type_b),
  type3: type_.GeneratedType(type_c),
  type4: type_.GeneratedType(type_d),
) -> type_.GeneratedType(
  CustomType(
    repr,
    #(
      #(
        #(
          #(#(), type_.GeneratedType(type_a)),
          type_.GeneratedType(type_b),
        ),
        type_.GeneratedType(type_c),
      ),
      type_.GeneratedType(type_d),
    ),
  ),
)
pub fn to_type5(
  input: CustomType(
    repr,
    #(
      #(
        #(
          #(
            #(#(), type_.GeneratedType(a)),
            type_.GeneratedType(b),
          ),
          type_.GeneratedType(c),
        ),
        type_.GeneratedType(d),
      ),
      type_.GeneratedType(e),
    ),
  ),
  type1: type_.GeneratedType(type_a),
  type2: type_.GeneratedType(type_b),
  type3: type_.GeneratedType(type_c),
  type4: type_.GeneratedType(type_d),
  type5: type_.GeneratedType(type_e),
) -> type_.GeneratedType(
  CustomType(
    repr,
    #(
      #(
        #(
          #(
            #(#(), type_.GeneratedType(type_a)),
            type_.GeneratedType(type_b),
          ),
          type_.GeneratedType(type_c),
        ),
        type_.GeneratedType(type_d),
      ),
      type_.GeneratedType(type_e),
    ),
  ),
)
pub fn with_dynamic_variants(
  old: CustomTypeBuilder(repr, old, generics),
  variants: fn(generics) -> List(variant.Variant(type_.Dynamic)),
) -> CustomTypeBuilder(repr, type_.Dynamic, generics)

Add a dynamic list of variants to the custom type, given its generics (see with_generic).

pub fn with_generic(
  old: CustomTypeBuilder(repr, variants, old_generics),
  generic: String,
) -> CustomTypeBuilder(
  repr,
  variants,
  #(old_generics, type_.GeneratedType(a)),
)

Add a generic type parameter to the custom type.

let my_option =
  custom.new()
  |> custom.with_generic("a")
  |> custom.with_variant(fn(generics) {
    let #(#(), a) = generics
    variant.new("MySome")
    |> variant.with_argument(option.None, a)
  })
  |> custom.with_variant(fn(_generics) {
    variant.new("MyNone")
  })
use my_option_type, my_some_constructor, my_none_constructor <- module.with_custom_type2(
  definition.new("MyOption"),
  my_option,
)
pub fn with_variant(
  old: CustomTypeBuilder(repr, old, generics),
  variant: fn(generics) -> variant.Variant(new),
) -> CustomTypeBuilder(repr, #(old, new), generics)

Add a variant the custom type, given its generics (see with_generic). See also with_dynamic_variants for adding multiple variants at once.

custom.new()
|> custom.with_variant(fn(_) {
  variant.new("Dog")
  |> variant.with_argument(option.Some("bones"), type_.int)
})
|> custom.with_variant(fn(_) {
  variant.new("Cat")
  |> variant.with_argument(option.Some("name"), type_.string)
  |> variant.with_argument(option.Some("has_catnip"), type_.bool)
})
Search Document