From: "zverok (Victor Shepelev) via ruby-core" <ruby-core@ml.ruby-lang.org>
To: ruby-core@ml.ruby-lang.org
Cc: "zverok (Victor Shepelev)" <noreply@ruby-lang.org>
Subject: [ruby-core:120293] [Ruby master Bug#20943] Constant defined in `Data.define` block
Date: Wed, 18 Dec 2024 07:58:17 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-111058.20241218075817.4@ruby-lang.org> (raw)
In-Reply-To: <redmine.issue-20943.20241211020608.4@ruby-lang.org>
Issue #20943 has been updated by zverok (Victor Shepelev).
TBH, for bigger `Struct`/`Data`-based classes I typically prefer a regular inheritance instead of using class definition block—both for just aestetics (“it looks like a class definition”) and to avoid behavioral differences like constant definition discussed here, or being able to treat `Data` like a proper base class:
For example, `.new` for `Data`-based classes accepts both positional and keyword arguments. But if one is _sure_ they want to change this behavior, with regular inheritance, it is extremely easy:
```ruby
class Unit < Data.define(:value)
def self.new(value, **nil) = super(value:)
end
Unit.new(1) #=> #<data Unit value=1>
Unit.new(value: 1)
# no keywords accepted (ArgumentError)
```
Such easy redefinition (using `super`) is not available with `Data.define(...) { ... }`.
For all I know, there are two counter-arguments for regular inheritance for those cases:
1. Creation of unnecessary anonymous immediate classes
2. Problems with code reloading
In my dayjob (large regular Rails applications) I find the former negligible, and never met with the latter, but this experience might not be universal.
Yet theoretically, I’d rather thought in the direction of changes that would make regular inheritance from `Data.define`/`Struct.new` less problematic (if it really is, and it is not just “how we are used to think of it”).
Just to notice here: Other than Struct/Data, there are other libraries that make use of “inheriting of dynamically produced classes” approach (though I didn’t look into the implementation details for many years, so I am not sure how it is implemented currently), like [Sequel](https://sequel.jeremyevans.net/rdoc/files/README_rdoc.html#label-Sequel+Models):
```ruby
class Post < Sequel::Model(:my_posts); end
```
or, IIRC, some of the dry-rb gems.
So, it might be, that “optimizing of inheritance from dynamic classes/modules” is more desirable decision than complication of block scopes.
----------------------------------------
Bug #20943: Constant defined in `Data.define` block
https://bugs.ruby-lang.org/issues/20943#change-111058
* Author: nobu (Nobuyoshi Nakada)
* Status: Open
* Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
From https://github.com/ruby/ruby/pull/12274:
> A couple times in code review I've seen constants inadvertently leak to top level from within a `Struct` or `Data` do block. I think it would be nice to show reopening the `Data` class when a constant is defined, so the constant is defined within the namespace. In this case, `Measure::NONE` instead of top level `Object::NONE`. It would also show readers that it's okay to reopen a `Data` class, which seems nice since some folk might not realize. Thanks for considering!
However, I think that `NONE` probably might be intended to be defined under `Measure`.
Current:
```ruby
Measure = Data.define(:amount, :unit) do
NONE = Data.define
end
p NONE #=> NONE
```
Another:
```ruby
Measure = Data.define(:amount, :unit) do
NONE = Data.define
p NONE #=> Measure::NONE
end
p NONE # uninitialized constant NONE (NameError)
```
@zverok How do think?
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
next prev parent reply other threads:[~2024-12-18 7:58 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-11 2:06 [ruby-core:120174] [Ruby master Bug#20943] Constant defined in `Data` block nobu (Nobuyoshi Nakada) via ruby-core
2024-12-11 10:29 ` [ruby-core:120176] [Ruby master Bug#20943] Constant defined in `Data.define` block byroot (Jean Boussier) via ruby-core
2024-12-11 18:56 ` [ruby-core:120182] " matheusrich (Matheus Richard) via ruby-core
2024-12-17 23:51 ` [ruby-core:120284] " shan (Shannon Skipper) via ruby-core
2024-12-18 6:19 ` [ruby-core:120291] " matz (Yukihiro Matsumoto) via ruby-core
2024-12-18 7:58 ` zverok (Victor Shepelev) via ruby-core [this message]
2024-12-24 20:16 ` [ruby-core:120400] " luke-gru (Luke Gruber) via ruby-core
2024-12-30 10:35 ` [ruby-core:120446] " byroot (Jean Boussier) via ruby-core
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-111058.20241218075817.4@ruby-lang.org \
--to=ruby-core@ml.ruby-lang.org \
--cc=noreply@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).