-
Notifications
You must be signed in to change notification settings - Fork 93
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
Implement user-defined type guard method #1501
Draft
tk0miya
wants to merge
1
commit into
soutaro:master
Choose a base branch
from
tk0miya:type_guard/self
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tk0miya
commented
Feb 19, 2025
05bd3c7
to
1e9f080
Compare
tk0miya
commented
Feb 20, 2025
Comment on lines
+752
to
+757
def context_from(type_name) | ||
if type_name.namespace == RBS::Namespace.root | ||
[nil, type_name] | ||
else | ||
parent = context_from(type_name.namespace.to_type_name) | ||
[parent, type_name] | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could not find such a helper method in Steep and RBS.
I'm not sure where the best place is.
d43291c
to
6821365
Compare
tk0miya
commented
Feb 24, 2025
d0f4440
to
835b944
Compare
This implements the minimal version of user-defined type guard method. A user-defined type guard methos is a method defined by user that is able to guarantee the type of the objects (receiver or arguments). At present, Steep supports type guard methods provided by ruby-core (ex. `#is_a?`, `#nil?`, and so on). But we also have many kinds of user-defined methods that are able to check the type of the objects. Therefore user-defined type guard will help checking the type of these applications by narrowing types. This implementation uses an annotation to declare user-defined type guard method. ``` class Example < Integer %a{guard:self is Integer} def integer?: () -> bool end ``` For example, the above method `Example#integer?` is a user-defined type guard method that narrows the Example object itself to an Integer if the conditional branch passed. ``` example = Example.new if example.integer? example #=> Integer end ``` In this PR, the predicate of type guards only supports "self is TYPE" statement. I have a plan to extend it: * `%a{guard:self is arg}` * `%a{guard:self is_a arg}` * `%a{guard:self is TYPE_PARAM}` * `%a{guard:arg is TYPE}` Note: The compatibility of RBS syntax is the large reason of using annotations. I'm afraid that adding a new syntax to define it will bring breaking change to the RBS, and difficult to use it on common repository or generators (ex. gem_rbs_collection and rbs_rails).
835b944
to
c1a708e
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This implements the minimal version of user-defined type guard method. A user-defined type guard methos is a method defined by user that is able to guarantee the type of the objects (receiver or arguments).
At present, Steep supports type guard methods provided by ruby-core (ex.
#is_a?
,#nil?
, and so on). But we also have many kinds of user-defined methods that are able to check the type of the objects.Therefore user-defined type guard will help checking the type of these applications by narrowing types.
This implementation uses an annotation to declare user-defined type guard method.
For example, the following method
Example#integer?
is a user-defined type guard method that narrows the Example object itself to an Integer if the conditional branch passed.In this PR, the predicate of type guards only supports "self is TYPE" statement. I have a plan to extend it:
%a{guard:self is arg}
%a{guard:self is_a arg}
%a{guard:self is TYPE_PARAM}
%a{guard:arg is TYPE}
Note: The compatibility of RBS syntax is the large reason of using annotations. I'm afraid that adding a new syntax to define it will bring breaking change to the RBS, and difficult to use it on common repository or generators (ex. gem_rbs_collection and rbs_rails).
refs: