Proposal: Add method annotations: primitive:nil? and primitive:not_nil? - #905
Proposal: Add method annotations: primitive:nil? and primitive:not_nil?#905tk0miya wants to merge 1 commit into
Conversation
To represent methods like `Object#blank?` and `Object#present?` in ActiveSupport, this adds method annotations `primitive:nil?` and `primitive:not_nil?`. They will be considered as same as `nil?` and not `nil?` internally. refs: soutaro#472
|
Someone in Ruby Discord asked if we can write “truthy”, i.e. exact opposite of |
|
The goal of this PR is to provide type narrowing to Steep. So this is not related to the "truthy" type that you mentioned. For example, the following code is available in Ruby on Rails: But current steep does not support this type narrowing. This PR adds a new feature to define type narrowing methods via |
|
If we defined class Object
%a{pure} def present?: () -> bool
end
class NilClass
%a{pure} def present?: () -> ::FalseClass
endIn the example below, shouldn't Steep have enough information to narrow name = params[:name] #=> String?
if name.present?
name #=> String
endThis doesn't appear to work currently, but it seems like it should. |
| def test_logic_receiver_is_nil_via_annotation | ||
| with_checker(<<-RBS) do |checker| | ||
| class Object | ||
| %a{primitive:nil?} |
There was a problem hiding this comment.
Unlike nil?, blank? doesn't guarantee the receiver is nil (e.g. it returns true for empty string)
To represent methods like
Object#blank?andObject#present?in ActiveSupport, this adds method annotationsprimitive:nil?andprimitive:not_nil?.They will be considered as same as
nil?and notnil?internally.refs: #472
Please let me know your opinion about implementation, the naming of annotations, and so on.