Skip to content
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

Merged
merged 3 commits into from
Mar 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3604,6 +3604,27 @@ end
Person = Struct.new(:first_name, :last_name)
----

=== 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.

[source,ruby]
----
# bad
class Person < Data.define(:first_name, :last_name)
end
Copy link
Collaborator

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.

Copy link
Contributor Author

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?


Person.ancestors
# => [Person, #<Class:0x0000000105abed88>, Data, Object, (...)]

# good
Person = Data.define(:first_name, :last_name)

Person.ancestors
# => [Person, Data, Object, (...)]
----

=== Duck Typing [[duck-typing]]

Prefer https://en.wikipedia.org/wiki/Duck_typing[duck-typing] over inheritance.
Expand Down