-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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 rule about not extending Data.define
#917
Add rule about not extending Data.define
#917
Conversation
I'd say yes.
In separate PR - yes. Let's keep the |
See also: * [PR adding new cop for this rule](rubocop/rubocop#11728) * [Original issue](rubocop/rubocop#11696)
1f15b25
to
dceb7bb
Compare
README.adoc
Outdated
=== Don't Extend `Data.define` [[no-extend-data-define]] | ||
|
||
Don't extend an instance initialized by `Data.define`. | ||
Extending it introduces a superfluous class level and may also introduce weird errors if the file is required multiple times. |
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.
introduce weird errors if the file is required multiple times
I don't really get this part.
class A < Data.define(:x); end
class A < Data.define(:x); end # => TypeError: superclass mismatch for class A
class B < Struct.new(:x); end
class B < Struct.new(:x); end # => TypeError: superclass mismatch for class B
Also, require
docs:
Loads the given name, returning true if successful and false if the feature is already loaded.
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.
Nice catch! I think I must've forgot to remove the part about introduce weird errors... when I copied and adjusted the description from the [[no-extend-struct-new]]
.
I guess now the question is - what did the author of [[no-extend-struct-new]]
meant there by weird errors and if it's still relevant 🤔
I'll remove the part after and, thank you!
Does it really? cc @zverok E = Data.define('Foo', :x)
Data::Foo # => NameError: uninitialized constant Data::Foo |
Alright, changed the order so
I don't get this part, sorry! Got confused hwew:
By there you mean this PR, or in general the |
See the discussion [here](rubocop#917 (comment))
---- | ||
# bad | ||
class Person < Data.define(:first_name, :last_name) | ||
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 think it will also be a good idea to show the output of Person.ancestors
in both examples to show clearly the anonymous class that gets added there.
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! Do you think I should open another one to illustrate this for [[no-extend-struct-new]]
too?
It does not. It did in initial implementation (for feature parity with Struct), but other core devs suggested that this function for Struct was legacy and there is no value in reproducing it in Data |
NB: I am not sure how valuable this input is, but there are cases when classic inheritance from |
See also:
I was wondering about two things in this PR:
[[no-extend-struct-new]]
rule. Decided to just append to the end of the guide eventually.[[struct-new]]
rule to usingData.define
instead. Kinda depends on the use case, but I'd assumeData.define
should be the default for rubies 3.2+ 🤔 Looking forward to the discussion here, I'm open to adding this change to this PR(or opening new PR for that)