Feature #21000
openA way to avoid loading constant required by a type check
Description
There is this pattern I encounter sometimes:
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
case obj
when nil
when defined?(XYZ)
if obj and defined?(XYZ) === obj
if defined?(Gem::Specification)&.respond_to?(:each)
Very versatile, with the downside that it's a small backward incompatibiliy.
Updated by nobu (Nobuyoshi Nakada) 2 days ago
Dan0042 (Daniel DeLorme) wrote:
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 likecase obj when nil when defined?(XYZ) if obj and defined?(XYZ) === obj if defined?(Gem::Specification)&.respond_to?(:each)
Very versatile, with the downside that it's a small backward incompatibiliy.
I like this, but we will need to consider the incompatibility and inconsistency with other constants.
Updated by Eregon (Benoit Daloze) 2 days ago · Edited
defined?
so far always returned nil
or frozen strings, so I think it's not so great to use for this.
Also defined?
is particularly error-prone for constants & autoloading, for instance:
$DEBUG = true # to show swallowed exceptions
autoload :Foo, "foo"
p defined?(Foo) # "constant"
p defined?(Foo::Bar)
# Exception 'LoadError' at <internal:/home/eregon/.rubies/ruby-3.4.1/lib/ruby/3.4.0/rubygems/core_ext/kernel_require.rb>:136 - cannot load such file -- foo
# Exception 'LoadError' at <internal:/home/eregon/.rubies/ruby-3.4.1/lib/ruby/3.4.0/rubygems/core_ext/kernel_require.rb>:144 - cannot load such file -- foo
nil
So defined?(Foo::Bar)
will actually try to load Foo
.
Updated by Dan0042 (Daniel DeLorme) 1 day ago
Eregon (Benoit Daloze) wrote in #note-2:
So
defined?(Foo::Bar)
will actually try to loadFoo
.
It would be a good idea to fix that right?
Updated by Eregon (Benoit Daloze) 1 day ago · Edited
I'm unsure, I suppose it could be a compatibility concern.
If you want to know if Foo::Bar
exists and Foo
is an autoload the only way to know is to load Foo and see if it has a constant Bar
.
So while surprising these semantics do make sense since defined?
doesn't have a "it's an autoload (not yet loaded), I don't know" return value, it's either nil
(doesn't exist) or "constant"
(exists).
That said, p defined?(Foo) # nil
is weird though.
Updated by Dan0042 (Daniel DeLorme) 1 day ago
While these semantics make a certain sense, the opposite also makes just as much sense. If Foo
is not yet autoloaded, that means neither Foo
nor Foo::Bar
are yet defined, and it makes sense to return falsy for both. At the very least defined?(Foo)
and defined?(Foo::Bar)
should be consistent.
Updated by mame (Yusuke Endoh) about 20 hours ago
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
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.
Updated by Dan0042 (Daniel DeLorme) about 14 hours ago · Edited
mame (Yusuke Endoh) wrote in #note-7:
When you create a ticket, please state clearly the problem you wish to solve.
Therefore, you want a way to write a decision similar to this idiom without firing autoload. Preferably without writing the constant name twice.
Therefore, I want a way to write a decision similar to this idiom without writing the constant name twice. Preferably without firing autoload.
Apologies if this wasn't clear enough in the original description—I may not have formatted it effectively.
In the third idea I mentioned autoload as a possible solution in the context of pattern matching, but I hadn't really considered autoload in the context of defined?(XYZ)
in the fourth idea.
To clarify, my fourth idea was:
defined?(NothingHere) #=> nil
N = nil
defined?(N) #=> "constant"
XYZ = Class.new
defined?(XYZ) #=> XYZ
I hadn’t accounted for how defined?
should behave with an autoloaded constant. @Eregon (Benoit Daloze) mentioned p defined?(Foo) # nil
but it actually returns "constant", which I wasn't aware of.
I think it would make sense for defined?(Foo)
to return nil
for a not-yet-loaded autoload constant. Alternatively, it could trigger autoload and return Foo
if it resolves to a module. But as you said that is no longer a small incompatibility.
Updated by Eregon (Benoit Daloze) about 14 hours ago · Edited
Dan0042 (Daniel DeLorme) wrote in #note-8:
I hadn’t accounted for how
defined?
should behave with an autoloaded constant. @Eregon (Benoit Daloze) mentionedp defined?(Foo) # nil
but it actually returns "constant", which I wasn't aware of.
Sorry about that, it is indeed "constant"
, I must have made a copy-paste mistake (I also updated the original post for clarity).
Here is the full code and output:
$ ruby -de 'autoload :Foo, "foo"; p defined?(Foo); p defined?(Foo::Bar)'
Exception `LoadError' at /home/eregon/.rubies/ruby-3.3.5/lib/ruby/3.3.0/rubygems.rb:1346 - cannot load such file -- rubygems/defaults/operating_system
Exception `LoadError' at /home/eregon/.rubies/ruby-3.3.5/lib/ruby/3.3.0/rubygems.rb:1363 - cannot load such file -- rubygems/defaults/ruby
"constant"
Exception `LoadError' at <internal:/home/eregon/.rubies/ruby-3.3.5/lib/ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:136 - cannot load such file -- foo
Exception `LoadError' at <internal:/home/eregon/.rubies/ruby-3.3.5/lib/ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:144 - cannot load such file -- foo
nil
So indeed defined?(Foo)
returns "constant"
without trying to load it, and defined?(Foo::Bar)
triggers the autoload.
Updated by Dan0042 (Daniel DeLorme) about 12 hours ago
After some further thought, I believe the fourth idea could work if autoload constants simply returned "constant". Because if a constant isn’t yet autoloaded, no objects of that type can exist. To recap:
defined?(NothingHere) #=> nil
N = nil
defined?(N) #=> "constant"
XYZ = Class.new
defined?(XYZ) #=> XYZ
autoload :Foo, "foo"
defined?(Foo) #=> "constant"
With this approach, we’re back to only a small incompatibility. I describe it as "small" because defined?
is almost always used as a boolean condition. I've only found 1 gem that uses the return value of defined?
beyond checking its truthiness, and even then, it's for "method", not "constant"
oj-3.13.14/test/helper.rb
22:if defined?(GC.verify_compaction_references) == 'method'
oj-3.13.14/test/json_gem/test_helper.rb
18: if defined?(GC.verify_compaction_references) == 'method'
Updated by mame (Yusuke Endoh) about 5 hours ago
If you just want to avoid repetition and don't care autoload, I found a good (and magical) way to do it.
class TrueClass
def true?
end
end
if defined?(obj.is_a?(NameSpace::ClassName).true?)
# obj is a NameSpace::ClassName
else
# obj is not a NameSpace::ClassName
end
# or, more magically
if defined?(-(obj.is_a?(NameSpace::ClassName)&&1))
# obj is a NameSpace::ClassName
else
# obj is not a NameSpace::ClassName
end
The behavior of defined?
that actually evaluates the some part is sometimes convenient :-)
I don't think my idiom is nice, though. It is too magical. And, I think if obj and defined?(XYZ) === obj
is too magical as well.