Skip to content

RFC: Generic aliases#28

Open
stakach wants to merge 4 commits into
crystal-lang:masterfrom
stakach:rfc/generic-aliases
Open

RFC: Generic aliases#28
stakach wants to merge 4 commits into
crystal-lang:masterfrom
stakach:rfc/generic-aliases

Conversation

@stakach
Copy link
Copy Markdown

@stakach stakach commented May 23, 2026

Rendered: https://github.com/stakach/rfcs/blob/rfc/generic-aliases/text/0028-generic-aliases.md

Summary

Allow alias declarations to take type parameters so a single declaration can stand in for a family of substituted types:

alias Maybe(T)        = T | Nil
alias StringKeyed(V)  = Hash(String, V)
alias Pair(K, V)      = Tuple(K, V)

A generic alias is usable everywhere a regular type is — as a restriction, in type declarations, inside generic instantiations, as a metaclass expression, and as a forall T restriction — and is purely substitutional (no new runtime type, no new dispatch).

Motivation

#2803 — today there's no way to name a parameterised shape without either copy-pasting per-variant aliases (MaybeInt32, MaybeString, …) or introducing a wrapper class with runtime cost. The RFC body covers prior art (Rust, Scala 3, TypeScript, Haskell) and the rationale for choosing transparent substitution over a nominal wrapper.

Status

Drafted at the level of user-visible semantics; intentionally avoids prescribing compiler internals so the implementation has room to land however reviewers think is cleanest. Splat parameters and parameter constraints are explicitly listed as out of scope / unresolved for follow-up RFCs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant