-
-
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 name to error message in non-nil asserting getters #8200
Add name to error message in non-nil asserting getters #8200
Conversation
The improved error message for getters is good. But I'm not in support of adding a custom error message to Besides, you can usually just raise a custom exception: foo.not_nil! "because bar"
foo || raise "because bar" This has the same size, but offers more flexibility because you can fully customize the exception being raised. |
Not trying to argue for the I'm not much of a fan of
nil.not_nil!("Nil not expected here") It's also shorter than writing: nil || raise NilAssertionError.new("Nil not expected here") Which is equivalent to what it does now. If you care about the exception type, then it gets lengthy. I prefer the "or raise" case myself. Or for a workaround, I used: if (parent = @parent)
parent.do_something
else
raise NilAssertionError.new("Parent node can't be nil")
end Where (foo.bar || raise "bar can't be nil").baz
# or...
if (b = foo.bar)
b.baz
else
raise "bar can't be nil"
end
# or even...
def bar_not_nil
foo.bar || raise "bar can't be nil"
end
bar_not_nil.baz It gets quite ugly. An alternative: foo.bar.not_nil!("bar can't be nil").baz This is shorter and cleaner, but still kind of weird having a message thrown in there. |
|
Update this PR to just have improved error messages for |
Not needed, the block variant of getter can already be used for that: class Klass
getter str : String { raise "oops" }
end Even if |
@j8r Improving the error message for @icy-arctic-fox New PR or this one, your choice. |
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.
Thanks @icy-arctic-fox 🙏
@straight-shoota why you consider this a breaking-change? The exception type raised is the same. The message changes but not its type. |
I think it would be better to somehow include the type name in the message, like " |
sth along |
@bcardiff Maybe because |
Adds the getter's name to the error message when the non-nil assertion fails. This resolves issue #6431 . Affects
getter!
,property!
,class_getter!
, andclass_property!
.