From: "mame (Yusuke Endoh) via ruby-core" <ruby-core@ml.ruby-lang.org>
To: ruby-core@ml.ruby-lang.org
Cc: "mame (Yusuke Endoh)" <noreply@ruby-lang.org>
Subject: [ruby-core:120519] [Ruby master Feature#21000] A way to avoid loading constant required by a type check
Date: Tue, 07 Jan 2025 10:18:03 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-111324.20250107101802.11019@ruby-lang.org> (raw)
In-Reply-To: <redmine.issue-21000.20250103184405.11019@ruby-lang.org>
Issue #21000 has been updated by mame (Yusuke Endoh).
When you create a ticket, please state clearly the problem you wish to solve.
Here is my understanding. You see *two* problems with the following idiom
```ruby
if defined?(Foo) and obj.is_a?(Foo)
```
* This idiom fires autoload. Before the actual load, there is no way that `obj` could be an instance of Foo, so we can tell "false" without firing autoload.
* It is redundant to write the constant name twice.
Therefore, you want a way to write a decision similar to this idiom without firing autoload. Preferably without writing the constant name twice.
Is my understanding right?
If so, how would your last proposal that `defined?(XYZ)` return `XYZ` solve this problem?
Currently, for the constant `XYZ` for which autoload is set, `defined?(XYZ)` returns `"constant"` even before the actual load.
If it were to return `XYZ` itself, I don't think it would solve the autoload problem.
Or does your proposal involve changing `defined?(XYZ)` to return `nil` if it is before the actual load? I don't think that is a small incompatibility.
----------------------------------------
Feature #21000: A way to avoid loading constant required by a type check
https://bugs.ruby-lang.org/issues/21000#change-111324
* Author: Dan0042 (Daniel DeLorme)
* Status: Open
----------------------------------------
There is this pattern I encounter sometimes:
```ruby
if defined?(NameSpace::ClassName) and obj.is_a?(NameSpace::ClassName)
```
Searching in gems, the pattern is fairly common: https://pastebin.com/VGfjRWNu
I would like a way to avoid the repetition of `NameSpace::ClassName` above. I can think of a number of ways to approach the issue, each with different tradeoffs...
### Pattern match ignores uninitialized constant
Pattern match like `obj in XYZ` could return false if XYZ is not defined. The danger here is that a typo could go undetected and just silently ignore the error even when the constants is expected to be defined.
### Pattern match has special syntax to ignore uninitialized constant
Pattern match such as `obj in XYZ?` (or some other syntax) could return false if XYZ is not defined. The downside is that we're adding yet more new syntax. But it could be `obj in defined?(XYZ)` and then it doesn't really feel like new syntax.
### Do not autoload constants required by pattern match
If we have `autoload :XYZ, "xyz"` then `obj in XYZ` could skip the autoload and return false. There is a possibility that `XYZ` might be defined as a regexp or other matcher that return true, but in general autoload is only used for classes/modules. And if the class/module is not yet loaded, obviously an object of that type cannot exist so we can avoid loading it. But this would only work for autoloaded constants, so can't be used to check a library that might not be loaded, ex: `obj in ActiveRecord::Base`
### defined?(mod) returns mod if it's a class/module
If XYZ is a module, `defined?(XYZ)` could return XYZ instead of returning "constant". So it can be used in expressions like
```ruby
case obj
when nil
when defined?(XYZ)
```
```ruby
if obj and defined?(XYZ) === obj
```
```ruby
if defined?(Gem::Specification)&.respond_to?(:each)
```
Very versatile, with the downside that it's a small backward incompatibiliy.
--
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:[~2025-01-07 10:18 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-03 18:44 [ruby-core:120469] " Dan0042 (Daniel DeLorme) via ruby-core
2025-01-06 2:17 ` [ruby-core:120492] " nobu (Nobuyoshi Nakada) via ruby-core
2025-01-06 9:40 ` [ruby-core:120496] " Eregon (Benoit Daloze) via ruby-core
2025-01-06 13:32 ` [ruby-core:120501] " Dan0042 (Daniel DeLorme) via ruby-core
2025-01-06 14:26 ` [ruby-core:120502] " Eregon (Benoit Daloze) via ruby-core
2025-01-06 14:32 ` [ruby-core:120503] " Dan0042 (Daniel DeLorme) via ruby-core
2025-01-07 10:18 ` mame (Yusuke Endoh) via ruby-core [this message]
2025-01-07 15:56 ` [ruby-core:120527] " Dan0042 (Daniel DeLorme) via ruby-core
2025-01-07 16:25 ` [ruby-core:120528] " Eregon (Benoit Daloze) via ruby-core
2025-01-07 18:43 ` [ruby-core:120532] " Dan0042 (Daniel DeLorme) via ruby-core
2025-01-08 1:46 ` [ruby-core:120544] " mame (Yusuke Endoh) via ruby-core
2025-01-08 9:55 ` [ruby-core:120554] " Eregon (Benoit Daloze) via ruby-core
2025-01-08 14:36 ` [ruby-core:120558] " Dan0042 (Daniel DeLorme) 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-111324.20250107101802.11019@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).