Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type should declare weakly distinct types instead of aliasing #15

Open
liquidev opened this issue Jan 5, 2022 · 0 comments
Open

type should declare weakly distinct types instead of aliasing #15

liquidev opened this issue Jan 5, 2022 · 0 comments
Labels
language feature Adding a new feature into the language.

Comments

@liquidev
Copy link
Member

liquidev commented Jan 5, 2022

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.

@liquidev liquidev added the language feature Adding a new feature into the language. label Jan 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
language feature Adding a new feature into the language.
Projects
None yet
Development

No branches or pull requests

1 participant