From: "shan (Shannon Skipper) via ruby-core" <ruby-core@ml.ruby-lang.org>
To: ruby-core@ml.ruby-lang.org
Cc: "shan (Shannon Skipper)" <noreply@ruby-lang.org>
Subject: [ruby-core:120284] [Ruby master Bug#20943] Constant defined in `Data.define` block
Date: Tue, 17 Dec 2024 23:51:21 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-111049.20241217235121.4@ruby-lang.org> (raw)
In-Reply-To: <redmine.issue-20943.20241211020608.4@ruby-lang.org>
Issue #20943 has been updated by shan (Shannon Skipper).
byroot (Jean Boussier) wrote in #note-2:
> Which is a bit wasteful as you define two classes instead of one, but not a big deal.
I agree the extra class being created is minor, but I'm slightly more bothered by the anonymous class in the ancestry.
```ruby
[Something,
#<Class:0x00000001438d5dd0>,
Struct,
...
]
```
> I kinda wish this would be valid syntax:
>
> ```ruby
> class Something = Struct.new(:a, :b)
> ...
> end
> ```
I'd like that too. That same pattern would be great for both `Struct` and `Data` from my vantage. Or if both could keep constants in scope within `do` blocks, but would that be consider breaking? Too late for Data? I'd like it.
It seems like options include:
1. Change `Data.define do` block scope for constants
2. Add a new `Something = Data.define` or similar syntax
3. Recommend reopening classes in docs
4. Recommend inheritance in docs
5. Keep the status quo of defining a constant outside the scope in docs
That ^ list happens to be roughly in my personal order of preference from top to bottom. :) Having both 1 and 2 would also be an option.
I wonder if changing documentation to something that keeps `NONE` inside the module is worth doing in the short term? If syntax adjustments are decided against, I'd rather just recommend reopening `Data` and `Struct` classes rather than the slight back bending with existing solutions like `self::` prefix or `< Data.define`. On the other hand, I'd love to see a syntax adjustment to make it easier to define a constant within a `Data` without reopening the class.
----------------------------------------
Bug #20943: Constant defined in `Data.define` block
https://bugs.ruby-lang.org/issues/20943#change-111049
* 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-17 23:51 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 ` shan (Shannon Skipper) via ruby-core [this message]
2024-12-18 6:19 ` [ruby-core:120291] " matz (Yukihiro Matsumoto) via ruby-core
2024-12-18 7:58 ` [ruby-core:120293] " zverok (Victor Shepelev) via ruby-core
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-111049.20241217235121.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).