From: "mame (Yusuke Endoh) via ruby-core" Date: 2025-01-08T01:46:08+00:00 Subject: [ruby-core:120544] [Ruby master Feature#21000] A way to avoid loading constant required by a type check Issue #21000 has been updated by mame (Yusuke Endoh). If you just want to avoid repetition and don't care autoload, I found a good (and magical) way to do it. ```ruby 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. ---------------------------------------- Feature #21000: A way to avoid loading constant required by a type check https://bugs.ruby-lang.org/issues/21000#change-111348 * 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/