ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "okuramasafumi (Masafumi OKURA)" <noreply@ruby-lang.org>
To: ruby-core@ml.ruby-lang.org
Subject: [ruby-core:111055] [Ruby master Feature#19099] Support `private_constant` for an undefined constant
Date: Tue, 29 Nov 2022 13:14:03 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-100315.20221129131403.34@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-19099.20221102051211.34@ruby-lang.org

Issue #19099 has been updated by okuramasafumi (Masafumi OKURA).


I wonder if the code below is acceptable:

```ruby
class C
  private_constant {
    X = ...
  }
end
```

 Here, `private_constant` takes a block and every constant defined there are private. This is especially useful when defining multiple private constants at once.

----------------------------------------
Feature #19099: Support `private_constant` for an undefined constant
https://bugs.ruby-lang.org/issues/19099#change-100315

* Author: ujihisa (Tatsuhiro Ujihisa)
* Status: Open
* Priority: Normal
----------------------------------------
All the following discussion applies to `public_constant` too. Maybe `deprecate_constant` as well.

## Problem

```ruby
class C
  X = ...
  private_constant :X
end
```

The above idiom usually works fine, but when `...` part is long, like a 30-line Ruby Hash, it's very easy to miss the following `private_constant :X` part.

## Impossible solution

```ruby
class C
  private_constant X = ...
end
```

Like `private`, if the above notation could work, it would be awesome, but it breaks so many backward compatibility. The constant assignment returns its value but not the name of the constant, and we should keep the current behaviour.

## Proposed solution

Allow the following new notation for `private_constant` by making constant private by name without actually resolving itself and raises an error.

``` ruby
class C
  private_constant :X
  X = ...
end
```

The current behaviour is to raise NameError.

```
/tmp/v8svpb4/95:2:in `private_constant': constant C::X1 not defined (NameError)

  private_constant :X1
  ^^^^^^^^^^^^^^^^
	from /tmp/v8svpb4/95:2:in `<class:C>'
	from /tmp/v8svpb4/95:1:in `<main>'
```

This proposal breaks this backward compatibility.

Also I'm concerned about potential typos. It may be hard to find typos.

```ruby
class C
  private_constant :BEHAVIOUR
  BEHAVIOR = 123 # Remains public unintentionally
end
```

Maybe we need some sort of foolproof somewhere in this way.



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

  parent reply	other threads:[~2022-11-29 13:14 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-02  5:12 [ruby-core:110578] [Ruby master Feature#19099] Support `private_constant` for an undefined constant ujihisa (Tatsuhiro Ujihisa)
2022-11-02  7:01 ` [ruby-core:110580] " znz (Kazuhiro NISHIYAMA)
2022-11-03 17:55 ` [ruby-core:110591] " Eregon (Benoit Daloze)
2022-11-03 19:21 ` [ruby-core:110592] " ujihisa (Tatsuhiro Ujihisa)
2022-11-03 20:31 ` [ruby-core:110596] " sawa (Tsuyoshi Sawada)
2022-11-29 13:14 ` okuramasafumi (Masafumi OKURA) [this message]
2022-11-30 17:55 ` [ruby-core:111094] " Dan0042 (Daniel DeLorme)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-100315.20221129131403.34@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    --cc=ruby-core@ml.ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).