Skip to content

feat: type aliasing with the alias keyword #2136

Description

@SchoolyB

Summary

Add type aliasing to Grayscale using the alias keyword. An alias creates a
new name for an existing type — purely for readability. The alias and the
original type are fully interchangeable.


Syntax

alias PlayerID = int
alias Point3D = Point

Private aliases (scoped to the declaring file):

private alias ID = int

Rules

  • File-scope only — no aliases inside functions, loops, or blocks
  • Public by default, private restricts to the declaring file
  • The alias and the underlying type are identical — fully interchangeable in all contexts
  • Can alias any type: primitives, structs, enums, function signatures, containers
  • No chained alias resolution issues — an alias of an alias just resolves to the base type

Compiler Changes

  • Lexer: Register alias as a keyword
  • Parser: Parse alias Name = Type as a new top-level declaration node
  • Typechecker: Resolve alias references to their underlying type; validate the target type exists
  • Codegen: Emit the underlying C type wherever the alias is used (aliases are erased at codegen)

Examples

alias Meters = float
alias Username = string

do calculate_distance(start Meters, end Meters) -> Meters {
    if end > start {
        return end - start
    }
    return start - end
}

do main() {
    mut distance Meters = calculate_distance(10.0, 25.5)
    println("Distance: ${distance}")
}

Since aliases are interchangeable, this also works:

mut distance float = calculate_distance(10.0, 25.5)

Metadata

Metadata

Assignees

No one assigned

    Labels

    codegenRelated to C code generationenhancementNew feature or requestgraycRelated to the grayscale compiler core or its internal toolingtypecheckerRelated to type checking and validation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions