gen_gleam/types
Provides built-in Gleam types. Provides means to define Gleam types (e.g. variant_type
) and generate Gleam code for those (generate_type_def
).
Types
pub type Field {
Field(name: String, field_type: GleamType)
}
Constructors
-
Field(name: String, field_type: GleamType)
pub type GleamType {
AnonymousType(String)
NilType
IntType
ListType(list_type: GleamType)
TypeDefType(type_def: TypeDef)
FunctionType(
argument_types: List(GleamType),
result_type: GleamType,
)
}
Constructors
-
AnonymousType(String)
-
NilType
-
IntType
-
ListType(list_type: GleamType)
-
TypeDefType(type_def: TypeDef)
-
FunctionType( argument_types: List(GleamType), result_type: GleamType, )
pub type TypeDef {
VariantType(name: String, variants: List(Variant))
VariantDepType(
name: String,
dep_types: List(GleamType),
variants: List(Variant),
)
}
Constructors
-
VariantType(name: String, variants: List(Variant))
-
VariantDepType( name: String, dep_types: List(GleamType), variants: List(Variant), )
Functions
pub fn dep_type_def(
name: String,
dep_types: List(GleamType),
variants: List(Variant),
) -> TypeDef
Returns the TypeDef
that you can pass to generate_type_def
. If you want the type,
use variant_dep_type
instead.
pub fn function(
argument_types: List(GleamType),
result_type: GleamType,
) -> GleamType
pub fn generate_type(arg_type: GleamType) -> String
Generate the type when using the argument. To be used when you annotate any pattern matching. It is possible this will be done automatically from the statement, in some future version.
pub fn generate_type_def(type_def: TypeDef) -> String
This is a root-statement
pub fn type_def(name: String, variants: List(Variant)) -> TypeDef
Returns the TypeDef
that you can pass to generate_type_def
. If you want the type,
use variant_type
instead.
pub fn variant_dep_type(
name: String,
dep_types: List(GleamType),
variants: List(Variant),
) -> GleamType
For a type definition, the additional types are wildcards
let a = anonymous("a")
variant_dep_type("MyType", [a], [
variant("ListOfIt", [field("the_list", list(a))])
])
|> generate_type_def
For usage, the additional types are concrete
let t = Int
variant_dep_type("MyType", [t], [
variant("ListOfIt", [field("the_list", list(t))])
])
|> generate_type
pub fn variant_type(
name: String,
variants: List(Variant),
) -> GleamType
variant_type("MyType", [
variant("Bare", []).
variant("WithField", [field("number", int())])
])