-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add TypeNode#has_method? #4474
Add TypeNode#has_method? #4474
Conversation
Nice addition, you should add some specs for this |
@bew there aren't any for the rest of related methods, I can add it though. |
@Sija yes, please let's add some spec in I would suggest also to split the fixes in the docs in other PR 🙏 Regarding the functionality, I have some doubts how it should interact with |
@@ -1533,6 +1538,15 @@ module Crystal | |||
ArrayLiteral.new(defs) | |||
end | |||
|
|||
def self.has_method?(type, name) | |||
type.defs.try &.each do |name, metadatas| |
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.
You can simply use the name
variable here, there's no need to traverse all metadatas
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.
@Sija Ping :-)
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.
BoolLiteral.new !!type.defs[name]?
will do?
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.
Yes, or type.defs.has_key?(name)
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.
Actually... type.has_def?(name)
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.
done :)
cde2491
to
f5cec6b
Compare
@@ -1533,6 +1538,10 @@ module Crystal | |||
ArrayLiteral.new(defs) | |||
end | |||
|
|||
def self.has_method?(type, name) | |||
BoolLiteral.new(!!type.has_def?(name)) |
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 think that BoolLiteral.new(type.has_def?(name))
is enough, as Type#has_def?
is already returning a boolean
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.
@bew nope, returned type is Bool | Nil
. I'd say it should indeed return just Bool
, yet it might be out of the scope of this PR.
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.
right, sorry about that (found why: some_hash.try &.has_key?(some_key)
can be Nil
with the try
..)
@Sija don't worry about that, it's a separate issue |
Is it GTG? Travis CI xcode8.2 build failed, rest 's good. |
Fixes #4472