gen_gleam/types
For generating Gleam types
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)
VariantType(name: String, variants: List(Variant))
VariantDepType(
name: String,
dep_types: List(GleamType),
variants: List(Variant),
)
FunctionType(
argument_types: List(GleamType),
result_type: GleamType,
)
}
Constructors
-
AnonymousType(String)
-
NilType
-
IntType
-
ListType(list_type: GleamType)
-
VariantType(name: String, variants: List(Variant))
-
VariantDepType( name: String, dep_types: List(GleamType), variants: List(Variant), )
-
FunctionType( argument_types: List(GleamType), result_type: GleamType, )
pub type Variant {
Variant(name: String, fields: List(Field))
}
Constructors
-
Variant(name: String, fields: List(Field))
Functions
pub fn anonymous(name: String) -> GleamType
pub fn field(name: String, field_type: GleamType) -> Field
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(gleam_type: GleamType) -> String
This is a root-statement
Use for your own type definitions, that is, variant_type
and variant_dep_type
.
Will panic when used on pre-defined types such as Int
(but not on Result(a, b)
a.t.m.)
For the moment, will generate a function declaration when passed a function.
pub fn int() -> GleamType
pub fn list(item_type: GleamType) -> GleamType
pub fn nil() -> GleamType
pub fn variant(name: String, fields: List(Field)) -> Variant
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())])
])