ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:114774] [Ruby master Feature#19884] Make Safe Navigation Operator work on classes
@ 2023-09-15  8:06 p8 (Petrik de Heus) via ruby-core
  2023-09-15  8:11 ` [ruby-core:114775] " sawa (Tsuyoshi Sawada) via ruby-core
                   ` (10 more replies)
  0 siblings, 11 replies; 13+ messages in thread
From: p8 (Petrik de Heus) via ruby-core @ 2023-09-15  8:06 UTC (permalink / raw
  To: ruby-core; +Cc: p8 (Petrik de Heus)

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/
 ______________________________________________
 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/

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:114775] [Ruby master Feature#19884] Make Safe Navigation Operator work on classes
  2023-09-15  8:06 [ruby-core:114774] [Ruby master Feature#19884] Make Safe Navigation Operator work on classes p8 (Petrik de Heus) via ruby-core
@ 2023-09-15  8:11 ` sawa (Tsuyoshi Sawada) via ruby-core
  2023-09-15  9:57 ` [ruby-core:114778] " rubyFeedback (robert heiler) via ruby-core
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 13+ messages in thread
From: sawa (Tsuyoshi Sawada) via ruby-core @ 2023-09-15  8:11 UTC (permalink / raw
  To: ruby-core; +Cc: sawa (Tsuyoshi Sawada)

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/
 ______________________________________________
 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/

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:114778] [Ruby master Feature#19884] Make Safe Navigation Operator work on classes
  2023-09-15  8:06 [ruby-core:114774] [Ruby master Feature#19884] Make Safe Navigation Operator work on classes p8 (Petrik de Heus) via ruby-core
  2023-09-15  8:11 ` [ruby-core:114775] " sawa (Tsuyoshi Sawada) via ruby-core
@ 2023-09-15  9:57 ` rubyFeedback (robert heiler) via ruby-core
  2023-10-07  2:24 ` [ruby-core:114966] " AMomchilov (Alexander Momchilov) via ruby-core
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 13+ messages in thread
From: rubyFeedback (robert heiler) via ruby-core @ 2023-09-15  9:57 UTC (permalink / raw
  To: ruby-core; +Cc: rubyFeedback (robert heiler)

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/
 ______________________________________________
 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/

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:114966] [Ruby master Feature#19884] Make Safe Navigation Operator work on classes
  2023-09-15  8:06 [ruby-core:114774] [Ruby master Feature#19884] Make Safe Navigation Operator work on classes p8 (Petrik de Heus) via ruby-core
  2023-09-15  8:11 ` [ruby-core:114775] " sawa (Tsuyoshi Sawada) via ruby-core
  2023-09-15  9:57 ` [ruby-core:114778] " rubyFeedback (robert heiler) via ruby-core
@ 2023-10-07  2:24 ` AMomchilov (Alexander Momchilov) via ruby-core
  2023-10-07  9:50 ` [ruby-core:114968] " p8 (Petrik de Heus) via ruby-core
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 13+ messages in thread
From: AMomchilov (Alexander Momchilov) via ruby-core @ 2023-10-07  2:24 UTC (permalink / raw
  To: ruby-core; +Cc: AMomchilov (Alexander Momchilov)

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/a455e9d55771f1e3dfea0cc4183e66f9632b431c/lib/rubocop/lockfile.rb#L31

----------------------------------------
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/
 ______________________________________________
 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/

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:114968] [Ruby master Feature#19884] Make Safe Navigation Operator work on classes
  2023-09-15  8:06 [ruby-core:114774] [Ruby master Feature#19884] Make Safe Navigation Operator work on classes p8 (Petrik de Heus) via ruby-core
                   ` (2 preceding siblings ...)
  2023-10-07  2:24 ` [ruby-core:114966] " AMomchilov (Alexander Momchilov) via ruby-core
@ 2023-10-07  9:50 ` p8 (Petrik de Heus) via ruby-core
  2023-10-07 10:00 ` [ruby-core:114969] " p8 (Petrik de Heus) via ruby-core
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 13+ messages in thread
From: p8 (Petrik de Heus) via ruby-core @ 2023-10-07  9:50 UTC (permalink / raw
  To: ruby-core; +Cc: p8 (Petrik de Heus)

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/railties/lib/rails/generators/named_base.rb#L139
https://github.com/rails/rails/blob/7f7f9df8641e35a076fe26bd097f6a1b22cb4e2d/railties/lib/rails/generators/generated_attribute.rb#L67C1-L76

```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/
 ______________________________________________
 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/

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:114969] [Ruby master Feature#19884] Make Safe Navigation Operator work on classes
  2023-09-15  8:06 [ruby-core:114774] [Ruby master Feature#19884] Make Safe Navigation Operator work on classes p8 (Petrik de Heus) via ruby-core
                   ` (3 preceding siblings ...)
  2023-10-07  9:50 ` [ruby-core:114968] " p8 (Petrik de Heus) via ruby-core
@ 2023-10-07 10:00 ` p8 (Petrik de Heus) via ruby-core
  2023-10-07 11:37 ` [ruby-core:114970] " sawa (Tsuyoshi Sawada) via ruby-core
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 13+ messages in thread
From: p8 (Petrik de Heus) via ruby-core @ 2023-10-07 10:00 UTC (permalink / raw
  To: ruby-core; +Cc: p8 (Petrik de Heus)

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/
 ______________________________________________
 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/

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:114970] [Ruby master Feature#19884] Make Safe Navigation Operator work on classes
  2023-09-15  8:06 [ruby-core:114774] [Ruby master Feature#19884] Make Safe Navigation Operator work on classes p8 (Petrik de Heus) via ruby-core
                   ` (4 preceding siblings ...)
  2023-10-07 10:00 ` [ruby-core:114969] " p8 (Petrik de Heus) via ruby-core
@ 2023-10-07 11:37 ` sawa (Tsuyoshi Sawada) via ruby-core
  2023-10-07 14:45 ` [ruby-core:114971] " p8 (Petrik de Heus) via ruby-core
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 13+ messages in thread
From: sawa (Tsuyoshi Sawada) via ruby-core @ 2023-10-07 11:37 UTC (permalink / raw
  To: ruby-core; +Cc: sawa (Tsuyoshi Sawada)

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/
 ______________________________________________
 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/

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:114971] [Ruby master Feature#19884] Make Safe Navigation Operator work on classes
  2023-09-15  8:06 [ruby-core:114774] [Ruby master Feature#19884] Make Safe Navigation Operator work on classes p8 (Petrik de Heus) via ruby-core
                   ` (5 preceding siblings ...)
  2023-10-07 11:37 ` [ruby-core:114970] " sawa (Tsuyoshi Sawada) via ruby-core
@ 2023-10-07 14:45 ` p8 (Petrik de Heus) via ruby-core
  2023-10-08  4:30 ` [ruby-core:114977] " sawa (Tsuyoshi Sawada) via ruby-core
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 13+ messages in thread
From: p8 (Petrik de Heus) via ruby-core @ 2023-10-07 14:45 UTC (permalink / raw
  To: ruby-core; +Cc: p8 (Petrik de Heus)

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/
 ______________________________________________
 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/

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:114977] [Ruby master Feature#19884] Make Safe Navigation Operator work on classes
  2023-09-15  8:06 [ruby-core:114774] [Ruby master Feature#19884] Make Safe Navigation Operator work on classes p8 (Petrik de Heus) via ruby-core
                   ` (6 preceding siblings ...)
  2023-10-07 14:45 ` [ruby-core:114971] " p8 (Petrik de Heus) via ruby-core
@ 2023-10-08  4:30 ` sawa (Tsuyoshi Sawada) via ruby-core
  2023-10-08  7:53 ` [ruby-core:114980] " nobu (Nobuyoshi Nakada) via ruby-core
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 13+ messages in thread
From: sawa (Tsuyoshi Sawada) via ruby-core @ 2023-10-08  4:30 UTC (permalink / raw
  To: ruby-core; +Cc: sawa (Tsuyoshi Sawada)

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/
 ______________________________________________
 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/

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:114980] [Ruby master Feature#19884] Make Safe Navigation Operator work on classes
  2023-09-15  8:06 [ruby-core:114774] [Ruby master Feature#19884] Make Safe Navigation Operator work on classes p8 (Petrik de Heus) via ruby-core
                   ` (7 preceding siblings ...)
  2023-10-08  4:30 ` [ruby-core:114977] " sawa (Tsuyoshi Sawada) via ruby-core
@ 2023-10-08  7:53 ` nobu (Nobuyoshi Nakada) via ruby-core
  2023-10-08 10:40 ` [ruby-core:114981] " p8 (Petrik de Heus) via ruby-core
  2023-10-10 14:01 ` [ruby-core:114993] " kddnewton (Kevin Newton) via ruby-core
  10 siblings, 0 replies; 13+ messages in thread
From: nobu (Nobuyoshi Nakada) via ruby-core @ 2023-10-08  7:53 UTC (permalink / raw
  To: ruby-core; +Cc: nobu (Nobuyoshi Nakada)

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/
 ______________________________________________
 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/

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:114981] [Ruby master Feature#19884] Make Safe Navigation Operator work on classes
  2023-09-15  8:06 [ruby-core:114774] [Ruby master Feature#19884] Make Safe Navigation Operator work on classes p8 (Petrik de Heus) via ruby-core
                   ` (8 preceding siblings ...)
  2023-10-08  7:53 ` [ruby-core:114980] " nobu (Nobuyoshi Nakada) via ruby-core
@ 2023-10-08 10:40 ` p8 (Petrik de Heus) via ruby-core
  2023-10-08 19:29   ` [ruby-core:114982] " Yaw Boakye via ruby-core
  2023-10-10 14:01 ` [ruby-core:114993] " kddnewton (Kevin Newton) via ruby-core
  10 siblings, 1 reply; 13+ messages in thread
From: p8 (Petrik de Heus) via ruby-core @ 2023-10-08 10:40 UTC (permalink / raw
  To: ruby-core; +Cc: p8 (Petrik de Heus)

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/

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:114982] Re: [Ruby master Feature#19884] Make Safe Navigation Operator work on classes
  2023-10-08 10:40 ` [ruby-core:114981] " p8 (Petrik de Heus) via ruby-core
@ 2023-10-08 19:29   ` Yaw Boakye via ruby-core
  0 siblings, 0 replies; 13+ messages in thread
From: Yaw Boakye via ruby-core @ 2023-10-08 19:29 UTC (permalink / raw
  To: Ruby developers; +Cc: Yaw Boakye


[-- Attachment #1.1: Type: text/plain, Size: 1976 bytes --]

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/

[-- Attachment #1.2: Type: text/html, Size: 2899 bytes --]

[-- Attachment #2: Type: text/plain, Size: 264 bytes --]

 ______________________________________________
 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/

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:114993] [Ruby master Feature#19884] Make Safe Navigation Operator work on classes
  2023-09-15  8:06 [ruby-core:114774] [Ruby master Feature#19884] Make Safe Navigation Operator work on classes p8 (Petrik de Heus) via ruby-core
                   ` (9 preceding siblings ...)
  2023-10-08 10:40 ` [ruby-core:114981] " p8 (Petrik de Heus) via ruby-core
@ 2023-10-10 14:01 ` kddnewton (Kevin Newton) via ruby-core
  10 siblings, 0 replies; 13+ messages in thread
From: kddnewton (Kevin Newton) via ruby-core @ 2023-10-10 14:01 UTC (permalink / raw
  To: ruby-core; +Cc: kddnewton (Kevin Newton)

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/
 ______________________________________________
 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/

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2023-10-10 14:01 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-15  8:06 [ruby-core:114774] [Ruby master Feature#19884] Make Safe Navigation Operator work on classes p8 (Petrik de Heus) via ruby-core
2023-09-15  8:11 ` [ruby-core:114775] " sawa (Tsuyoshi Sawada) via ruby-core
2023-09-15  9:57 ` [ruby-core:114778] " rubyFeedback (robert heiler) via ruby-core
2023-10-07  2:24 ` [ruby-core:114966] " AMomchilov (Alexander Momchilov) via ruby-core
2023-10-07  9:50 ` [ruby-core:114968] " p8 (Petrik de Heus) via ruby-core
2023-10-07 10:00 ` [ruby-core:114969] " p8 (Petrik de Heus) via ruby-core
2023-10-07 11:37 ` [ruby-core:114970] " sawa (Tsuyoshi Sawada) via ruby-core
2023-10-07 14:45 ` [ruby-core:114971] " p8 (Petrik de Heus) via ruby-core
2023-10-08  4:30 ` [ruby-core:114977] " sawa (Tsuyoshi Sawada) via ruby-core
2023-10-08  7:53 ` [ruby-core:114980] " nobu (Nobuyoshi Nakada) via ruby-core
2023-10-08 10:40 ` [ruby-core:114981] " p8 (Petrik de Heus) via ruby-core
2023-10-08 19:29   ` [ruby-core:114982] " Yaw Boakye via ruby-core
2023-10-10 14:01 ` [ruby-core:114993] " kddnewton (Kevin Newton) via ruby-core

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 read-only IMAP folder(s) and NNTP newsgroup(s).