[ruby-core:95517] [Ruby master Feature#16276] For consideration: "private do...end" / "protected do...end"
From:
shevegen@...
Date:
2019-10-23 21:44:18 UTC
List:
ruby-core #95517
Issue #16276 has been updated by shevegen (Robert A. Heiler).
Actually I should clarify some of my statements a bit more; I'll do it in a=
terse add-on.
- I believe that many ruby users may actually NOT indent when using private=
/public.
They may write code like this:
class Foo
def bla
end
private
def hop
end
end
I believe in these cases, the suggestion here may possibly be of less value=
to this
style, because they may prefer the current style more than any alternative.
An alternative may be:
class Foo
def bla
end
private
def hop
end
end
This is a bit "off" because the indent to private is one level to the left,=
but I see
this style sometimes as well. IMO I think an additional requirement for thi=
s suggestion,
although I think it is an ok-suggestion, should be how many ruby users may =
want to use
e. g. private {} in the first place. If there are only a very few then perh=
aps this may
not be worth to add/change it (again, not assuming anything either way, but=
I think the
usability and practical use cases should be considered too).
- I am mostly neutral on the whole issue actually, only a very slight +1 su=
pport. As =
said I probably may not need this, but this of course does not exclude the =
possibility
that others may use/want this. :)
It may be helpful if other ruby users could comment on the issue too in the=
coming
days/weeks, including people from the core team if they write lots of ruby =
code in
general.
----------------------------------------
Feature #16276: For consideration: "private do...end" / "protected do...end"
https://bugs.ruby-lang.org/issues/16276#change-82291
* Author: adh1003 (Andrew Hodgkinson)
* Status: Open
* Priority: Normal
* Assignee: =
* Target version: =
----------------------------------------
Private or protected declarations in Ruby classes are problematic. The sing=
le, standalone `public`, `private` or `protected` statements cause all foll=
owing methods - *except* "private" class methods, notably - to have that pr=
otection level. It is not idiomatic in Ruby to indent method definitions af=
ter such declarations, so it becomes at a glance very hard to see what a me=
thod's protection level is when just diving into a piece of source code. On=
e 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 thi=
s). Forcibly indenting code after declarations helps, but most Ruby develop=
ers find this unfamiliar and most auto-formatters/linters will reset it or,=
at best, complain. Further, the difficulty in defining private *class* met=
hods or constants tells us that perhaps there's more we should do here - bu=
t 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`, `p=
rivate` or `protected` declarations to - *optionally* - support a block-lik=
e 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 =3D "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 se=
mantics apply, allowing one to add new block-based protection-enclosed meth=
od declarations inside any existing code base without fear of accidentally =
changing the protection level of any methods defined below the new block. A=
s 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=EFve - for example, a=
rising unanswered questions include:
* Is the use of a block-like syntax making unwarranted assumptions about wh=
at the Ruby compiler can do during its various parsing phases?
* Does the use of a block-like syntax imply we should support things like P=
rocs too? (I *think* probably not - I see this as just syntax sugar to prov=
ide a new feature reusing a familiar idiom but without diving down any othe=
r 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 coul=
d be sketched out, then I might be able to have a go at implementation myse=
lf 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=3Dunsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>