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

Feature request: Type Parameter Bounds #829

Closed
tlubz opened this issue Nov 17, 2021 · 2 comments
Closed

Feature request: Type Parameter Bounds #829

tlubz opened this issue Nov 17, 2021 · 2 comments

Comments

@tlubz
Copy link

tlubz commented Nov 17, 2021

It's currently difficult to write a well-typed generic container that calls any methods on members of the type parameter. Example:

class TestContainer
  attr_reader :obj

  def initialize(obj)
    @obj = obj
  end

  def ==(other)
    @obj == other.obj
  end
end
class TestContainer[T]
  attr_reader obj: T
  def initialize: (T obj) -> void
  def ==: (untyped other) -> bool
end

steep (default settings) output:

steep check domain/TestContainer.rb 
# Type checking files:

..........................................................F

domain/TestContainer.rb:9:9: [error] Type `T` does not have method `==`
│ Diagnostic ID: Ruby::NoMethod
│
└     @obj == other.obj
           ~~

Detected 1 problem from 1 file

Ideally we would be able to assert at least one-sided bounds on the type parameters, for example if we could assert that the parameter inherits from Numeric:

class TestContainer[T < Numeric]
  attr_reader obj: T
  def initialize: (T obj) -> void
  def ==: (untyped other) -> bool
end
@soutaro
Copy link
Member

soutaro commented Nov 20, 2021

Hi @tlubz! 👋

Yes, we know the requirement of type parameter bounds and have it in our roadmap to RBS 2.0, which will be released with Ruby 3.1 hopefully.
I've started implementation of the feature and will share the progress on this repo soon.

@soutaro soutaro modified the milestone: RBS 2.0 Nov 20, 2021
@pocke
Copy link
Member

pocke commented Dec 31, 2021

This feature has been implemented in #844. So I'll close this issue.

@pocke pocke closed this as completed Dec 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants