In most programs it's more useful to declare a distinct type rather than aliasing an existing type. Say you're implementing an entity component system for a video game: you represent each entity with an ID:
type Entity = Size
Converting from a Size to an Entity should be explicit, as we're adding information by asserting that this Size is a valid entity. Converting from an Entity to a Size is less harmful, because although it loses information, any Entity is a valid Size, so no invariants are broken.
How the conversions should be performed - I'm still not sure. The simplest syntax for that would be Entity(1) - using a type like a function call. Converting from the weakly distinct type to the base type is implicit, so no syntax is needed, but just in case the compiler needs extra type information, we can use Size(an_entity) for consistency.
All implementations from the base type are available on the alias type, and since the existing operations on the base type know nothing of the alias, they still continue to operate and produce the base type. Referring back to the entity example, adding two entities using + is perfectly legal, but it produces a Size and not an Entity, so a conversion must be performed, like so: Entity(Entity(1) + Entity(1))
Converting from a base type to a type alias should only be possible within the package that declares the type, as we don't want external packages breaking invariants.
In most programs it's more useful to declare a distinct type rather than aliasing an existing type. Say you're implementing an entity component system for a video game: you represent each entity with an ID:
Converting from a Size to an Entity should be explicit, as we're adding information by asserting that this Size is a valid entity. Converting from an Entity to a Size is less harmful, because although it loses information, any Entity is a valid Size, so no invariants are broken.
How the conversions should be performed - I'm still not sure. The simplest syntax for that would be
Entity(1)
- using a type like a function call. Converting from the weakly distinct type to the base type is implicit, so no syntax is needed, but just in case the compiler needs extra type information, we can useSize(an_entity)
for consistency.All implementations from the base type are available on the alias type, and since the existing operations on the base type know nothing of the alias, they still continue to operate and produce the base type. Referring back to the entity example, adding two entities using
+
is perfectly legal, but it produces aSize
and not anEntity
, so a conversion must be performed, like so:Entity(Entity(1) + Entity(1))
Converting from a base type to a type alias should only be possible within the package that declares the type, as we don't want external packages breaking invariants.