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

Add a section on subtyping #547

Open
beta-ziliani opened this issue Sep 15, 2021 · 4 comments
Open

Add a section on subtyping #547

beta-ziliani opened this issue Sep 15, 2021 · 4 comments

Comments

@beta-ziliani
Copy link
Member

To start with, given that probably the semantics are still a bit in flux, it can be a stub with just the basics.

@HertzDevil
Copy link
Contributor

I worked on this a bit a while ago, but never got around submitting a PR because I'm not even sure if the docs are the most appropriate place for them. The rules are:

  • Idempotency: every type is a subtype of itself.
  • Transitivity: if T is a subtype of U and U is a subtype of V, then T is also a subtype of V.
  • A class or struct type is a subtype of its superclass.
  • A type is a subtype of the modules it includes.
  • A class type is a subtype of the modules its instance type extends.
  • A generic instance type is a subtype of its corresponding uninstantiated generic type.
  • If T and U are instances of Tuple, then T is a subtype of U if the two types have the same number of elements and each element type in T is a subtype of the corresponding element type in U.
  • If T and U are instances of NamedTuple, then T is a subtype of U if the two types have the same keys and each value type in T is a subtype of the corresponding value type in U.
  • A type T is a subtype of a union if T is a subtype of at least one variant type of that union.
  • A union type is a subtype of type T if all variant types of that union are subtypes of T.
  • An alias type is a subtype of the type it refers to, and vice-versa.
  • If T is a typedef of U, then T and U are sibling types; T is a subtype of U's supertypes, but neither T nor U is a subtype of one other.
  • T.class is a subtype of U.class if T is a subclass of U, or if T is a generic instance of U. (Subtyping rules for metaclasses are pending a decision for Ancestors of module metaclasses are inconsistent crystal#11110.)
  • NoReturn is a subtype of any type.

There would be examples for each of those rules using TypeNode#<=.

@beta-ziliani
Copy link
Member Author

If not in the book, where? I mean, some examples could go into a tutorial, but I wouldn't expect it to cover all these rules, just the most used ones.

@straight-shoota
Copy link
Member

I think the language reference is exactly where this needs to be documented.

@revskill10
Copy link

I'm confused a bit with subclass is a subtype of class.

Actually subclassing and subtyping are orthogonal concepts ?

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

No branches or pull requests

4 participants