A type alias gives an existing type a clearer name. It does not create a new type.
When to use a type alias
Use a type alias when a type annotation would be repetitive or when a name carries domain meaning.
UserId as I32
HtmlFragment as String
RouteParts as {String}
Aliases are transparent
An alias is another spelling of the same type.
UserId as I32
id UserId = 42
raw I32 = id
id has type UserId, but it is also an I32. You can pass it anywhere an I32 is expected.
UserId as I32 makes annotations clearer, but it does not stop an ordinary I32 from being used as UserId.
Alias versus struct
A struct creates a brand new type with its own fields and constructor. An alias does not.
UserId as I32
User = |
id I32,
|
UserId is I32. User is a separate type.
One collection alias
An alias can name a collection type.
Names as {String}
names Names = {"Priya", "Rob"}
A type alias is a compile-time name for an existing type. It introduces no runtime value and no new semantic type identity.
Type alias syntax
UserId as I32
Names as {String}
MaybeName as String?
StringBox as Box of String
The syntax is AliasName as ExistingType. Alias names follow type-name casing. The target may be any valid type expression that resolves to a concrete semantic type.
Exact type alias contract
- A type alias is a compile-time name for an existing type.
- Aliases are transparent: an alias and its target denote the same
TypeId. - Aliases do not create nominal identity.
- Alias and target values are type-compatible in both directions.
- Aliases are valid in type positions such as parameters, fields, returns and local annotations.
- Aliases are not ordinary values. They cannot be assigned, passed, copied or used in expression position.
as in a type-alias declaration is not a cast.
Valid targets
An alias target may be:
- a builtin scalar such as
I32, U32, F64, Bool, Char or String - a struct or choice declared in the same file
- an option such as
String? - a growable collection such as
{String} - a fixed collection such as
{4 I32} - a hashmap such as
{String = I32} - a fully concrete generic instance such as
Box of String - a dependency-bound type, dependency-bound type alias or external package type
Concrete generic aliases are supported. Parameterized aliases and partial generic application are outside the current language scope.
Transparency
UserId as I32
id UserId = 42
raw I32 = id
Because UserId is transparent, assigning a UserId value to an I32 binding is valid. The reverse is also valid: an I32 value can be used where UserId is expected.
Contrast with a struct
UserId as I32
User = |
id I32,
|
UserId is another name for I32. User is a distinct nominal struct type with its own constructor and fields. Use a struct when the domain concept needs its own identity. Use an alias when only the annotation name matters.
Alias versus nominal domain type
An alias improves spelling and readability, but it doesn't isolate a domain value from its target type. UserId as I32 still allows an ordinary I32 where UserId is expected. Use a wrapper struct today when distinct nominal identity matters.
Moth has no accepted primitive-backed nominal wrapper syntax. Such a wrapper remains an open design question and any future mechanism would stay separate from transparent as aliases.
Aliases may target externally provided types when those types are valid in type positions.
Empty literals and aliases
An alias can provide the explicit element type needed by an empty collection or map literal.
Names as {String}
names Names = {}
Alias chains
Aliases may chain through other aliases. The chain remains transparent and resolves to the final target type.
Name as String
Names as {Name}
names Names = {"Priya"}
Cycles
Alias cycles are invalid. The compiler reports a circular dependency.
Aliases are not constructors
An alias does not create a new constructor. The alias name is not callable even when the alias targets a constructible nominal type. Construct the target type using its canonical nominal name.
For struct aliases, only the struct's canonical name constructs:
Person = |
name String,
|
MainPerson as Person
person MainPerson = Person("Priya")
For choice aliases, only the choice's canonical name constructs variants:
Status ::
Ready,
Failed | message String |,
;
State as Status
ready State = Status::Ready
failed State = Status::Failed("bad")
Aliases to scalar or otherwise non-constructible targets are not callable for the same reason: the alias name is a type, not a value.
Visibility and module public surfaces
Type aliases follow ordinary module visibility. Same-module files can bind a helper's alias. To expose it across a module boundary, use $export on its declaration in a facade or on a direct-selection clause that re-exports it. A file-local dependency alias alone remains private.
Concepts related to type aliases
- For concrete generic application see Generics.
- For dependency aliasing see the next section.
- For project visibility and module roots see Project Structure and Packages.