ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: merch-redmine@jeremyevans.net
To: ruby-core@ruby-lang.org
Subject: [ruby-core:95609] [Ruby master Feature#16276] For consideration: "private do...end" / "protected do...end"
Date: Thu, 31 Oct 2019 15:10:27 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-82398.20191031151027.2d0f7a03ba7d0ef4@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-16276.20191023194923@ruby-lang.org

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


Dan0042 (Daniel DeLorme) wrote:
> Also I'd like to ask how fibers are relevant to this? When the `private` method toggles the visibility state of the current class/module, does that have anything to do with fibers?

```ruby
class A
end

Thread.new do
  class A
    def pub; end
  end
end

class A
  private do
    def priv; end
  end
end
```

Depending on thread timing, it is theoretically possible for pub to be private and not public if the visibility stored in the class and not in some sort of scope.  This example uses a thread, but the same basic issue applies to fibers.

----------------------------------------
Feature #16276: For consideration: "private do...end" / "protected do...end"
https://bugs.ruby-lang.org/issues/16276#change-82398

* Author: adh1003 (Andrew Hodgkinson)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
Private or protected declarations in Ruby classes are problematic. The single, standalone `public`, `private` or `protected` statements cause all following methods - *except* "private" class methods, notably - to have that protection level. It is not idiomatic in Ruby to indent method definitions after such declarations, so it becomes at a glance very hard to see what a method's protection level is when just diving into a piece of source code. One must carefully scroll *up* the code searching for a relevant declaration (easily missed, when everything's at the same indentation level) or have an IDE sufficiently advanced to give you that information automatically (and none of the lightweight editors I prefer personally have yet to support this). Forcibly indenting code after declarations helps, but most Ruby developers find this unfamiliar and most auto-formatters/linters will reset it or, at best, complain. Further, the difficulty in defining private *class* methods or constants tells us that perhaps there's more we should do here - but of course, we want to maintain backwards compatibility.

On the face of it, I can't see much in the way of allowing the `public`, `private` or `protected` declarations to - *optionally* - support a block-like syntax.

```
class Foo

  # ...there may be prior old-school public/private/protected declarations...

  def method_at_whatever_traditional_ruby_protection_level_applies
    puts "I'm traditional"
  end

  private do
    def some_private_instance_method
      puts "I'm private"
    end

    def self.some_private_class_method
      puts "I'm also private - principle of least surprise"
    end

    NO_NEED_FOR_PRIVATE_CONSTANT_DECLARATIONS_EITHER = "private"
  end

  def another_method_at_whatever_traditional_ruby_protection_level_applies
    puts "I'm also traditional"
  end

end
```

My suggestion here confines all `public do...end`, `protected do...end` or `private do...end` protections strictly to the confines of the block alone. Outside the block - both before and after - traditional Ruby protection semantics apply, allowing one to add new block-based protection-enclosed method declarations inside any existing code base without fear of accidentally changing the protection level of any methods defined below the new block. As noted in the pseudocode above, we can clean up some of the issues around the special syntax needed for "private constants", too.

I see a lot of wins in here but I'm aware I may be naïve - for example, arising unanswered questions include:

* Is the use of a block-like syntax making unwarranted assumptions about what the Ruby compiler can do during its various parsing phases?
* Does the use of a block-like syntax imply we should support things like Procs too? (I *think* probably not - I see this as just syntax sugar to provide a new feature reusing a familiar idiom but without diving down any other rabbit holes, at least not in the first implementation)

I've no idea how one would go about implementing this inside Ruby Core, as I've never tackled that before. If someone is keen to pick up the feature, great! Alternatively, if a rough idea of how it *might* be implemented could be sketched out, then I might be able to have a go at implementation myself and submit a PR - assuming anyone is keen on the idea in the first place `:-)`




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

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

  parent reply	other threads:[~2019-10-31 15:10 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-16276.20191023194923@ruby-lang.org>
2019-10-23 19:49 ` [ruby-core:95514] [Ruby master Feature#16276] For consideration: "private do...end" / "protected do...end" ahodgkin
2019-10-23 21:17 ` [ruby-core:95516] " shevegen
2019-10-23 21:44 ` [ruby-core:95517] " shevegen
2019-10-24  1:58 ` [ruby-core:95523] " duerst
2019-10-24  2:19 ` [ruby-core:95524] " shyouhei
2019-10-24  6:29 ` [ruby-core:95527] " ruby-core
2019-10-29 20:38 ` [ruby-core:95582] " ahodgkin
2019-10-29 21:17 ` [ruby-core:95583] " eregontp
2019-10-29 21:51 ` [ruby-core:95584] " merch-redmine
2019-10-30  1:42 ` [ruby-core:95585] " shyouhei
2019-10-30 19:08 ` [ruby-core:95591] " ahodgkin
2019-10-30 20:04 ` [ruby-core:95593] " merch-redmine
2019-11-01  3:21   ` [ruby-core:95626] " Austin Ziegler
2019-10-30 21:14 ` [ruby-core:95595] " XrXr
2019-10-31  5:03 ` [ruby-core:95600] " mame
2019-10-31  5:27 ` [ruby-core:95601] " duerst
2019-10-31 13:35 ` [ruby-core:95607] " daniel
2019-10-31 15:10 ` merch-redmine [this message]
2019-10-31 17:43 ` [ruby-core:95612] " daniel
2019-10-31 18:08 ` [ruby-core:95613] " merch-redmine
2019-10-31 19:11 ` [ruby-core:95617] " daniel
2019-10-31 19:30 ` [ruby-core:95618] " XrXr
2019-11-01  1:54 ` [ruby-core:95622] " daniel
2019-11-01  2:42 ` [ruby-core:95624] " shyouhei
2019-11-07  0:02 ` [ruby-core:95732] " ahodgkin
2019-11-07  5:09 ` [ruby-core:95740] " mame
2019-11-07 14:43 ` [ruby-core:95743] " daniel
2019-11-19  0:59 ` [ruby-core:95880] " ahodgkin
2019-11-19  4:40   ` [ruby-core:95882] " Martin J. Dürst
2019-11-19  4:27 ` [ruby-core:95881] " merch-redmine

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-82398.20191031151027.2d0f7a03ba7d0ef4@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
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).