On this page

0026. Newtypes are erased nominal wrappers

Context

Opaque IDs need to be distinct from their representation in the type system, while adding no runtime layout or cost. They also need familiar construction and extraction syntax and must compose with records, unions and generics.

Decision

Declare a nongeneric newtype as type UserId = new i64. new remains an identifier and is recognized contextually in this declaration. A type alias without new, such as type UserId = i64, is rejected (E0420); generic newtypes are rejected (E0421).

The newtype name enters both the type and function namespaces; construction is UserId(42). A conflicting function declaration is E0203. The sole field is .value, which yields the representation. Equality is nominal: values compare only with the same newtype. Arithmetic, ordering, or comparison with the representation is a type mismatch (E0301, with help to use .value); printing remains restricted to i64, bool and str (E0309). with does not apply.

In generic type checking, a newtype remains distinct from its representation; both erase to the same Core IR type. Construction erases to its argument and .value to its base, preserving left-to-right evaluation and side effects. No new layout or IR operation is introduced. Specializations such as id<UserId> and id<i64> remain distinct instances even though their lowered bodies have the same representation.

Constructors are public in this milestone. M9.1 adds module visibility and private constructors, making newtype IDs opaque across module boundaries.

Alternatives considered

Consequences

Newtypes can be used in records, unions and generic calls without changing their runtime layout. M4.6 adds E0420/E0421 within the record/newtype range; E0301 and E0309 cover invalid operations and printing. M9.1 must enforce constructor visibility across modules.

For compatibility with the required byte-identical pre-existing goldens, E0405 retains its legacy record-only wording instead of the M4 plan's proposed rewording. The registry clarifies that newtype .value is also supported.