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

Cannot specify type variables for instance #1521

Open
ParadoxV5 opened this issue Sep 16, 2023 · 6 comments
Open

Cannot specify type variables for instance #1521

ParadoxV5 opened this issue Sep 16, 2023 · 6 comments
Labels
wontfix This will not be worked on

Comments

@ParadoxV5
Copy link
Contributor

class MyWrapper[E]
  def self.of: (E) -> instance # RBS::UnknownTypeName (it’s not inferred from `MyWrapper[E]`)
  def self.of: (E) -> instance[E] # RBS::SyntaxError
  def self.of: [E] (E) -> instance # ok, but `[E]` is not used in practice (it’s not the same `[E]` as the one in `MyWrapper[E]`)
  def self.of: (untyped) -> instance # ok, but `untyped` is bad
end

Even Steep is aware of this limitation:
https://github.com/soutaro/steep/blob/v1.5.3/lib/steep/subtyping/check.rb#L307

Proposal: instance[E]?

The problem might be significant for class as well:
https://github.com/soutaro/steep/blob/v1.5.3/lib/steep/subtyping/check.rb#L322

@soutaro
Copy link
Member

soutaro commented Sep 16, 2023

I know there are some cases that requires that kind of types, but we don't have a plan to support it at least for now.

A class inheriting from a generic class may be non-generic, and what can we do for instance[T] (and maybe self[T]) types?

class Generic[T]
  def foo: () -> instance[Array[T]]
end

class NonGeneric < Generic[String]
end

# Even worse if it inherited by another generic class.
class AnotherGeneric[T] < NonGeneric
end

@soutaro soutaro added the wontfix This will not be worked on label Sep 16, 2023
@ParadoxV5
Copy link
Contributor Author

ParadoxV5 commented Sep 16, 2023

def self.of: (E) -> instance # RBS::UnknownTypeName (it’s not inferred from `MyWrapper[E]`)

How about letting class methods pick up generics?

@ParadoxV5
Copy link
Contributor Author

def self.of: (E) -> instance # RBS::UnknownTypeName (it’s not inferred from `MyWrapper[E]`)

How about letting class methods pick up generics?

Would this rather be a Steep job? Unless the RBS gem itself also involved…

@soutaro
Copy link
Member

soutaro commented Sep 28, 2023

How about letting class methods pick up generics?

Generics in RBS currently is for instance types.

I think no other programming language allows this. Do you know any example?

@soutaro
Copy link
Member

soutaro commented Sep 28, 2023

The type you want would be this?

def self.of: [E, T < MyWrapper[E]] (E) -> T

Steep allows explicit type application, and it will help:

MyWrapper.of(123) #$ Integer, AnotherWrapper         # E=Integer, T=AnotherWrapper

@ParadoxV5
Copy link
Contributor Author

The type you want would be this?

def self.of: [E, T < MyWrapper[E]] (E) -> T

Almost. The strength of instance is that it indicates the ::of of MyWrapperSubclass[E] < MyWrapper[E] returns a MyWrapperSubclass[E] and not an unspecific MyWrapper[E].

Steep allows explicit type application, and it will help:

MyWrapper.of(123) #$ Integer, AnotherWrapper         # E=Integer, T=AnotherWrapper

Good to know more 👍. Steep really lacks detailed and locatable docs for Ruby annotations, both in-line and #@type styles.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Development

No branches or pull requests

2 participants