-
Notifications
You must be signed in to change notification settings - Fork 86
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
Generics Don't Respond Properly to Common Methods or Nil Checks #994
Comments
You need to add constraints over type variables to call some method with them. class Optional[T < _NilP]
interface _NilP
def nil?: () -> bool
end
end Note that the def or_else(default)
if value = @value
value
else
default
end
end |
I think the problem is that class Optional[T < Object] # or [T < Kernel]
@value: T?
def initialize: (?T? value) -> void
def or_else: (T default) -> T?
end |
@soutaro's solution works like a charm, I think the only limitation is that it is unable to handle an Optional that is passed a boolean value. (Basically, because the check can't meaningfully differentiate between nil and false because both are falsey) |
Got it... Hmm, possible but weird workaround would be using def or_else(default)
case value
when NilClass
default
else
value
end
end But I don't think I want to recommend it... |
The case-when solution doesn't seem to work as that check doesn't seem to properly cast the nil off in the else block. It was a nice idea though |
It does appear that .nil? does work when the value is set to a local variable however, just not when it's an instance variable |
Yeah, it's complicated. Type narrowing works for local variables and some of the method calls, but not for instance variables. No unwrap in else-block in case-when example might be a bug.... |
Hi all. I'm currently working on trying to implement a generic class that has a nilable instance variable of the generic type, however Steep is having issues with handling the generic type.
There a two issues at hand here as far as I can tell. The first is that generic variables despite all types in Ruby being children of the Object type, do not recognize that they do have access to these methods. The same goes for interfaces as well. This means you can't use methods like
nil?
oris_a?
to verify the type in question.The second issue here is that if you add a constraint to the parameter to have it derive from Object it refuses to update the type from being optional to not optional, which means a method that say performs a nil check and then returns the non-nil value refuses to work correctly.
Below is a minimal example of my code:
And this is the RBS
These are some of the errors I get:
The text was updated successfully, but these errors were encountered: