[ruby-core:114774] [Ruby master Feature#19884] Make Safe Navigation Operator work on classes

Issue #19884 has been reported by p8 (Petrik de Heus). ---------------------------------------- Feature #19884: Make Safe Navigation Operator work on classes https://bugs.ruby-lang.org/issues/19884 * Author: p8 (Petrik de Heus) * Status: Open * Priority: Normal ---------------------------------------- If a class might not be defined we need to add a conditional: ```ruby defined?(ActiveRecord::Base) && ActiveRecord::Base.some_method ``` It would be nice if we could use the Safe Navigation Operator instead. ```ruby ActiveRecord::Base&.some_method ``` -- https://bugs.ruby-lang.org/

Issue #19884 has been updated by sawa (Tsuyoshi Sawada). Currently, you can do `ActiveRecord::Base.some_method rescue nil` Using `&.` for rescuing an error changes its meaning. ---------------------------------------- Feature #19884: Make Safe Navigation Operator work on classes https://bugs.ruby-lang.org/issues/19884#change-104616 * Author: p8 (Petrik de Heus) * Status: Open * Priority: Normal ---------------------------------------- If a class might not be defined we need to add a conditional: ```ruby defined?(ActiveRecord::Base) && ActiveRecord::Base.some_method ``` It would be nice if we could use the Safe Navigation Operator instead. ```ruby ActiveRecord::Base&.some_method ``` -- https://bugs.ruby-lang.org/

Issue #19884 has been updated by rubyFeedback (robert heiler). I am not sure that pattern is that common to warrant using &., aside from the syntax not being very elegant, but that may be my own bias against it (syntax-wise that is). I don't seem to need to do "defined?(Foo::Bar) && Foo::Bar.some_method". Is it that common to use "defined?()"? For behaviour change I tend to use .respond_to? or even .is_a? more often. ---------------------------------------- Feature #19884: Make Safe Navigation Operator work on classes https://bugs.ruby-lang.org/issues/19884#change-104619 * Author: p8 (Petrik de Heus) * Status: Open * Priority: Normal ---------------------------------------- If a class might not be defined we need to add a conditional: ```ruby defined?(ActiveRecord::Base) && ActiveRecord::Base.some_method ``` It would be nice if we could use the Safe Navigation Operator instead. ```ruby ActiveRecord::Base&.some_method ``` -- https://bugs.ruby-lang.org/

Issue #19884 has been updated by AMomchilov (Alexander Momchilov). @rubyFeedback It's common in library code, that want's to be flexible to different configurations of the target application. E.g. https://github.com/rubocop/rubocop/blob/a455e9d55771f1e3dfea0cc4183e66f9632b... ---------------------------------------- Feature #19884: Make Safe Navigation Operator work on classes https://bugs.ruby-lang.org/issues/19884#change-104838 * Author: p8 (Petrik de Heus) * Status: Open * Priority: Normal ---------------------------------------- If a class might not be defined we need to add a conditional: ```ruby defined?(ActiveRecord::Base) && ActiveRecord::Base.some_method ``` It would be nice if we could use the Safe Navigation Operator instead. ```ruby ActiveRecord::Base&.some_method ``` -- https://bugs.ruby-lang.org/

Issue #19884 has been updated by p8 (Petrik de Heus). There's multiple examples in the Rails code base: https://github.com/rails/rails/blob/7f7f9df8641e35a076fe26bd097f6a1b22cb4e2d... https://github.com/rails/rails/blob/7f7f9df8641e35a076fe26bd097f6a1b22cb4e2d... ```ruby def valid_type?(type) DEFAULT_TYPES.include?(type.to_s) || !defined?(ActiveRecord::Base) || ActiveRecord::Base.connection.valid_type?(type) end ``` It would be nice to rewrite this as: ```ruby def valid_type?(type) DEFAULT_TYPES.include?(type.to_s) || ActiveRecord&::Base&.connection.valid_type?(type) end ``` ---------------------------------------- Feature #19884: Make Safe Navigation Operator work on classes https://bugs.ruby-lang.org/issues/19884#change-104840 * Author: p8 (Petrik de Heus) * Status: Open * Priority: Normal ---------------------------------------- If a class might not be defined we need to add a conditional: ```ruby defined?(ActiveRecord::Base) && ActiveRecord::Base.some_method ``` It would be nice if we could use the Safe Navigation Operator instead. ```ruby ActiveRecord::Base&.some_method ``` -- https://bugs.ruby-lang.org/

Issue #19884 has been updated by p8 (Petrik de Heus). sawa (Tsuyoshi Sawada) wrote in #note-1:
Currently, you can do `ActiveRecord::Base.some_method rescue nil`
That would also rescue UndefinedMethodError, ArgumentError, and any error occuring in `some_method`.
Furthermore, the specification you are asking for is not clear. What Exception classes do you want `&.` to rescue?
`NameError`
You stated that you want `&.` to work on classes, but that does not make sense. If `ActiveRecord::Base` is not defined, it is not a class. How would Ruby know that it is (supposed to be) a class?
I have no idea how `&.` is currently implemented, but `defined?` also handles undefined classes. ---------------------------------------- Feature #19884: Make Safe Navigation Operator work on classes https://bugs.ruby-lang.org/issues/19884#change-104841 * Author: p8 (Petrik de Heus) * Status: Open * Priority: Normal ---------------------------------------- If a class might not be defined we need to add a conditional: ```ruby defined?(ActiveRecord::Base) && ActiveRecord::Base.some_method ``` It would be nice if we could use the Safe Navigation Operator instead. ```ruby ActiveRecord::Base&.some_method ``` -- https://bugs.ruby-lang.org/

Issue #19884 has been updated by sawa (Tsuyoshi Sawada). p8 (Petrik de Heus) wrote in #note-5:
Furthermore, the specification you are asking for is not clear. What Exception classes do you want `&.` to rescue?
`NameError`
Then you should edit and write that in the description.
`defined?` also handles undefined classes.
Can you show an example of that? ---------------------------------------- Feature #19884: Make Safe Navigation Operator work on classes https://bugs.ruby-lang.org/issues/19884#change-104842 * Author: p8 (Petrik de Heus) * Status: Open * Priority: Normal ---------------------------------------- If a class might not be defined we need to add a conditional: ```ruby defined?(ActiveRecord::Base) && ActiveRecord::Base.some_method ``` It would be nice if we could use the Safe Navigation Operator instead. ```ruby ActiveRecord::Base&.some_method ``` -- https://bugs.ruby-lang.org/

Issue #19884 has been updated by p8 (Petrik de Heus). sawa (Tsuyoshi Sawada) wrote in #note-6:
Then you should edit and write that in the description.
I've updated the description. Hopefully it's more clear now.
`defined?` also handles undefined classes.
Can you show an example of that?
```ruby defined?(UndefinedConstant) # returns nil instead of raising an error ``` ---------------------------------------- Feature #19884: Make Safe Navigation Operator work on classes https://bugs.ruby-lang.org/issues/19884#change-104844 * Author: p8 (Petrik de Heus) * Status: Open * Priority: Normal ---------------------------------------- If a constant isn't defined it will raise a NameError: ```ruby DoesNotExist.some_method # raises: uninitialized constant DoesNotExist (NameError) ``` In libraries that have optional dependencies, we can check if the constant is defined before calling a method on it: ```ruby defined?(OptionalDependency) && OptionalDependency.some_method ``` Currently in Ruby, the Safe Navigation Operator is used to avoid NoMethodError exceptions when calling methods on objects that may be nil. It would be nice if we could use the Safe Navigation Operator to avoid NameError on undefined constants as well. ```ruby ClassThatMightNotExist&.some_method ``` -- https://bugs.ruby-lang.org/

Issue #19884 has been updated by sawa (Tsuyoshi Sawada). sawa (Tsuyoshi Sawada) wrote:
You stated that you want &. to work on classes, but that does not make sense. If ActiveRecord::Base is not defined, it is not a class. How would Ruby know that it is (supposed to be) a class?
p8 (Petrik de Heus) wrote:
`defined?` also handles undefined classes.
sawa (Tsuyoshi Sawada) wrote:
Can you show an example of that?
p8 (Petrik de Heus) wrote:
```ruby defined?(UndefinedConstant) # returns nil instead of raising an error ```
Where does it say that `UndefinedConstant` is an "undefined **class** " rather than an undefined constant? ---------------------------------------- Feature #19884: Make Safe Navigation Operator work on classes https://bugs.ruby-lang.org/issues/19884#change-104849 * Author: p8 (Petrik de Heus) * Status: Open * Priority: Normal ---------------------------------------- If a constant isn't defined it will raise a NameError: ```ruby DoesNotExist.some_method # raises: uninitialized constant DoesNotExist (NameError) ``` In libraries that have optional dependencies, we can check if the constant is defined before calling a method on it: ```ruby defined?(OptionalDependency) && OptionalDependency.some_method ``` Currently in Ruby, the Safe Navigation Operator is used to avoid NoMethodError exceptions when calling methods on objects that may be nil. It would be nice if we could use the Safe Navigation Operator to avoid NameError on undefined constants as well. ```ruby ClassThatMightNotExist&.some_method ``` -- https://bugs.ruby-lang.org/

Issue #19884 has been updated by nobu (Nobuyoshi Nakada). What you wan doesn't seem the extension of the safe navigation operator. p8 (Petrik de Heus) wrote:
If a constant isn't defined it will raise a NameError:
```ruby DoesNotExist.some_method # raises: uninitialized constant DoesNotExist (NameError) ```
In libraries that have optional dependencies, we can check if the constant is defined before calling a method on it:
```ruby defined?(OptionalDependency) && OptionalDependency.some_method
```
Rather the extension of `defined?` operator which returns the value (instead of the expression type string) or nil. Let's call it `defined⁉️` for now. Isn't this what you want? ```ruby defined⁉️(OptionalDependency)&.some_method ```
Currently in Ruby, the Safe Navigation Operator is used to avoid NoMethodError exceptions when calling methods on objects that may be nil. It would be nice if we could use the Safe Navigation Operator to avoid NameError on undefined constants as well.
```ruby ClassThatMightNotExist&.some_method
```
This can't work because `&.` will works on the **result** of the LHS, which might have raised already. ---------------------------------------- Feature #19884: Make Safe Navigation Operator work on classes https://bugs.ruby-lang.org/issues/19884#change-104852 * Author: p8 (Petrik de Heus) * Status: Open * Priority: Normal ---------------------------------------- If a constant isn't defined it will raise a NameError: ```ruby DoesNotExist.some_method # raises: uninitialized constant DoesNotExist (NameError) ``` In libraries that have optional dependencies, we can check if the constant is defined before calling a method on it: ```ruby defined?(OptionalDependency) && OptionalDependency.some_method ``` Currently in Ruby, the Safe Navigation Operator is used to avoid NoMethodError exceptions when calling methods on objects that may be nil. It would be nice if we could use the Safe Navigation Operator to avoid NameError on undefined constants as well. ```ruby ClassThatMightNotExist&.some_method ``` -- https://bugs.ruby-lang.org/

Issue #19884 has been updated by p8 (Petrik de Heus). nobu (Nobuyoshi Nakada) wrote in #note-10:
Rather the extension of `defined?` operator which returns the value (instead of the expression type string) or nil.
Let's call it `defined⁉️` for now. Isn't this what you want?
```ruby defined⁉️(OptionalDependency)&.some_method ```
Yes, that would be nice. ---------------------------------------- Feature #19884: Make Safe Navigation Operator work on classes https://bugs.ruby-lang.org/issues/19884#change-104853 * Author: p8 (Petrik de Heus) * Status: Open * Priority: Normal ---------------------------------------- If a constant isn't defined it will raise a NameError: ```ruby DoesNotExist.some_method # raises: uninitialized constant DoesNotExist (NameError) ``` In libraries that have optional dependencies, we can check if the constant is defined before calling a method on it: ```ruby defined?(OptionalDependency) && OptionalDependency.some_method ``` Currently in Ruby, the Safe Navigation Operator is used to avoid NoMethodError exceptions when calling methods on objects that may be nil. It would be nice if we could use the Safe Navigation Operator to avoid NameError on undefined constants as well. ```ruby ClassThatMightNotExist&.some_method ``` -- https://bugs.ruby-lang.org/

How may `defined!?` be different from `Object#const_get` which is able to lookup and load a constant given its name? On Sun 8 Oct 2023 at 11:41, p8 (Petrik de Heus) via ruby-core < ruby-core@ml.ruby-lang.org> wrote:
Issue #19884 has been updated by p8 (Petrik de Heus).
nobu (Nobuyoshi Nakada) wrote in #note-10:
Rather the extension of `defined?` operator which returns the value (instead of the expression type string) or nil.
Let's call it `defined⁉️` for now. Isn't this what you want?
```ruby defined⁉️(OptionalDependency)&.some_method ```
Yes, that would be nice.
---------------------------------------- Feature #19884: Make Safe Navigation Operator work on classes https://bugs.ruby-lang.org/issues/19884#change-104853
* Author: p8 (Petrik de Heus) * Status: Open * Priority: Normal ---------------------------------------- If a constant isn't defined it will raise a NameError:
```ruby DoesNotExist.some_method # raises: uninitialized constant DoesNotExist (NameError) ```
In libraries that have optional dependencies, we can check if the constant is defined before calling a method on it:
```ruby defined?(OptionalDependency) && OptionalDependency.some_method
```
Currently in Ruby, the Safe Navigation Operator is used to avoid NoMethodError exceptions when calling methods on objects that may be nil. It would be nice if we could use the Safe Navigation Operator to avoid NameError on undefined constants as well.
```ruby ClassThatMightNotExist&.some_method
```
-- 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/postorius/lists/ruby-core.ml.ruby-lang.org...

Issue #19884 has been updated by kddnewton (Kevin Newton). @nobu has said this already, but just to reiterate, if we changed `Foo&.bar` to do nothing in the case that it was undefined, it would break things like zeitwerk and other libraries that handle const_missing. ---------------------------------------- Feature #19884: Make Safe Navigation Operator work on classes https://bugs.ruby-lang.org/issues/19884#change-104863 * Author: p8 (Petrik de Heus) * Status: Open * Priority: Normal ---------------------------------------- If a constant isn't defined it will raise a NameError: ```ruby DoesNotExist.some_method # raises: uninitialized constant DoesNotExist (NameError) ``` In libraries that have optional dependencies, we can check if the constant is defined before calling a method on it: ```ruby defined?(OptionalDependency) && OptionalDependency.some_method ``` Currently in Ruby, the Safe Navigation Operator is used to avoid NoMethodError exceptions when calling methods on objects that may be nil. It would be nice if we could use the Safe Navigation Operator to avoid NameError on undefined constants as well. ```ruby ClassThatMightNotExist&.some_method ``` -- https://bugs.ruby-lang.org/
participants (7)
-
AMomchilov (Alexander Momchilov)
-
kddnewton (Kevin Newton)
-
nobu (Nobuyoshi Nakada)
-
p8 (Petrik de Heus)
-
rubyFeedback (robert heiler)
-
sawa (Tsuyoshi Sawada)
-
Yaw Boakye