ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / Atom feed
From: merch-redmine@jeremyevans.net
To: ruby-core@ruby-lang.org
Subject: [ruby-core:95767] [Ruby master Feature#16336] Allow private constants to be accessed with absolute references
Date: Fri, 08 Nov 2019 23:26:49 +0000 (UTC)
Message-ID: <redmine.journal-82583.20191108232648.1b67c090392a4900@ruby-lang.org> (raw)
In-Reply-To: <redmine.issue-16336.20191108205406@ruby-lang.org>

Issue #16336 has been updated by jeremyevans0 (Jeremy Evans).


This would make private constant handling inconsistent with private method handling:

```
class A
  def self.b
    1
  end
  private_class_method :b

  A.b
  # NoMethodError (private method `b' called for A:Class)
end
```

That by itself isn't a reason to reject this feature, but it is something that should be considered.

----------------------------------------
Feature #16336: Allow private constants to be accessed with absolute references
https://bugs.ruby-lang.org/issues/16336#change-82583

* Author: dylants (Dylan Thacker-Smith)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
The purpose of constant privacy is to keep a constant from being accessed from outside the namespace.  As such, it can be surprising at first when something like the following doesn't work

```ruby
# in foo/a.rb
module Foo
  class A
  end
  private_constant :A
end

# in foo/b.rb
module Foo
  class B
    Foo::A # => NameError: private constant Foo::A referenced
  end
end
```

Once the unexpected behaviour is understood, it can be worked around by using a relative constant reference (changing `Foo::A` to `A` in the example).  However, the developer isn't just writing the code for the machine to understand, they are also writing it so it will be clear to other developers, where using the parent namespace in the constant reference may be appropriate for the readability of the code.

Instead, I think a reference to a private constant should be allowed through its parent namespace (as is the case with absolute constant references) if the parent namespace is in `Module.nesting` and the constant reference isn't made through a dynamic value (e.g. `foo = Foo; foo::A` wouldn't be allowed in the above example).

This will have the additional advantage that it will be easier to make a constant private, since these private constant references through their parent namespace won't have to be updated to remove the parent namespace from the constant reference.

I've opened pull request https://github.com/ruby/ruby/pull/2664 with a fix for this issue.



-- 
https://bugs.ruby-lang.org/

      parent reply index

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-16336.20191108205406@ruby-lang.org>
2019-11-08 20:54 ` [ruby-core:95764] " dylan.smith
2019-11-08 23:26 ` merch-redmine [this message]

Reply instructions:

You may reply publically 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-82583.20191108232648.1b67c090392a4900@ruby-lang.org \
    --to=ruby-core@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

ruby-core@ruby-lang.org archive (unofficial mirror)

Archives are clonable: git clone --mirror https://public-inbox.org/ruby-core

Example config snippet for mirrors

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.lang.ruby.core
	nntp://ou63pmih66umazou.onion/inbox.comp.lang.ruby.core
	nntp://news.gmane.org/gmane.comp.lang.ruby.core

 note: .onion URLs require Tor: https://www.torproject.org/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git