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

Bounded generics #844

Merged
merged 12 commits into from
Dec 23, 2021
Merged

Bounded generics #844

merged 12 commits into from
Dec 23, 2021

Conversation

soutaro
Copy link
Member

@soutaro soutaro commented Dec 15, 2021

This PR is to implement bounded generics.

Now you can specify the upper bound of type parameters with < symbol.

class PrettyPrint[T < _Output]
  interface _Output
    def <<: (String) -> void
  end

  attr_reader output: T
end

type str_printer = PrettyPrint[String]   # OK
type int_printer = PrettyPrint[Integer]  # Type error
  • The upper bound must be one of the class instance type, class singleton type, and interface type.
  • Generic method can also have upper bound.

Todos

  • Implement Writer
  • Implement type name resolution in upper bounds
  • Generic method type in Definition of generic classes
  • Implement validation to detect circular dependency between type parameters
  • Update docs

@soutaro soutaro force-pushed the typeparams branch 2 times, most recently from 1fb5325 to a118a40 Compare December 17, 2021 00:39
@soutaro soutaro marked this pull request as ready for review December 21, 2021 13:53
mame added a commit to ruby/typeprof that referenced this pull request Dec 23, 2021
ruby/rbs#844 changes the API of type parameters.
It will be included in RBS 2.0. This change will deal with the
incompatibilty in advance.
@soutaro soutaro merged commit 78da3e6 into master Dec 23, 2021
@soutaro soutaro deleted the typeparams branch December 23, 2021 11:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant