8000 Include "self type" methods like Enumerable#each from RBS files by apiology · Pull Request #769 · castwide/solargraph · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Include "self type" methods like Enumerable#each from RBS files #769

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

Merged
merged 2 commits into from

Mar 13, 2025

Conversation

apiology
Copy link
Contributor
@apiology apiology commented Mar 6, 2025

RBS has the concept of a 'self type', which is a way to refer to the requirements a class must implement in order to use a mix-in. An example would be the 'each' method, which you must implement before you can import 'Enumerable'.

The implication of this is that is it reasonably safe to assume that anything that includes Enumerable will have this method, which allows 'Enumerable' to be treated as a de-facto superclass of concrete container classes like 'Hash', 'Set' and 'Array' and allow methods to depend on the Enumerable abstract interface and accept a variety of container types.

With this change, we assume that if we receive something with type X, it implements all of its self types. As a result, we won't see typechecking errors when e.g. using #each on an Enumerable.

A future typechecking improvement would double check that the concrete class does implement the self type interface when the receiving type requires it.

RBS has the concept of a 'self type', which is a way to refer to the
requirements a class must implement in order to use a mix-in.  An
example would be the 'each' method, which you must implement before
you can import 'Enumerable'.

The implication of this is that is it reasonably safe to assume that
anything that includes Enumerable will have this method, which allows
'Enumerable' to be treated as a de-facto superclass of concrete
container classes like 'Hash', 'Set' and 'Array' and allow methods to
depend on the Enumerable abstract interface and accept a variety of
container types.

With this change, we assume that if we receive something with type X,
it implements all of its self types.  As a result, we won't see
typechecking errors when e.g. using #each on an Enumerable.

A future typechecking improvement would double check that the concrete
class does implement the self type interface when the receiving type
requires it.
apiology added a commit to apiology/solargraph that referenced this pull request Mar 10, 2025
Combined with castwide#769, this
allows the _Each#each self-type-method block's parameter to be typed.

This also sets the stage for future generic references (e.g.,
subclassing Array<T>).
@castwide castwide merged commit df588a8 into castwide:master Mar 13, 2025
9 checks passed
castwide pushed a commit that referenced this pull request Mar 15, 2025
* Add support for generic includes via RBS

Combined with #769, this
allows the _Each#each self-type-method block's parameter to be typed.

This also sets the stage for future generic references (e.g.,
subclassing Array<T>).

* Make code Ruby 2.6 compatible

* Fix bugs found during use
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

Successfully merging this pull request may close these issues.

2 participants
0