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) about 24 hours 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) about 16 hours ago
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) # nil
p defined?(Foo::Bar)
# Exception 'LoadError' at /home/eregon/.rubies/ruby-3.4.1/lib/ruby/3.4.0/rubygems.rb:1369 - cannot load such file -- rubygems/defaults/operating_system
# Exception 'LoadError' at /home/eregon/.rubies/ruby-3.4.1/lib/ruby/3.4.0/rubygems.rb:1386 - cannot load such file -- rubygems/defaults/ruby
# 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) about 12 hours 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) about 12 hours 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) about 11 hours 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.