4
$\begingroup$

Imagine a structural type system with the ability to create nominally-typed aliases:

type Foo = {foo: string, bar: number}

nominal type NominalFoo = Foo

Then the rules for assignability are simple: NominalFoo is always assignable to Foo, Foo can be downcast safely to NominalFoo, and Foo or other nominal aliases are not otherwise assignable to NominalFoo.

Is there anything about this which would cause problems? Particularly, does it make the implementation difficult, does it cause unsoundness, or are more assignability rules needed for this to be useful?

$\endgroup$
1

1 Answer 1

9
$\begingroup$

A nominal type is isomorphic to a structural record type with a language-guaranteed unique member.
For instance, I wrote a compiler where unions discriminate nominal types by treating them as containing a pointer to a string representing their name in ROM.
Virtual tables and pointers are another example of such a discriminant.
So yes, it is that easy.

Many languages have typesystems with both nominal and structural types.
For example,

  • TypeScript's types and interfaces are structural, but its enums are nominal,
  • Carbon's structs are structural, but its classes are nominal,
  • Go's structs are nominal, but its interfaces are structural,
  • Haskell's types are structural, but its newtypes are nominal.

Foo can be downcast safely to NominalFoo, and Foo or other nominal aliases are not otherwise assignable to NominalFoo.

Typically, such downcasts are possible but explicit.
This is especially desirable in languages marketing themselves as "low-level" if structural types expose a Row-Polymorphic behaviour where { x: a, y: b } <: { y: b, x: a, z: c }, as such a downcast requires use of a conversion routine and cannot rely on simple memcpy, though that has more to do with the structural part than the nominal.
An example of where this is not relevant but still requires explicit casts is Skew (under §Wrapped Types).

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .