Project

General

Profile

Actions

Feature #19099

open

Support `private_constant` for an undefined constant

Added by ujihisa (Tatsuhiro Ujihisa) over 1 year ago. Updated over 1 year ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:110578]

Description

All the following discussion applies to public_constant too. Maybe deprecate_constant as well.

Problem

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

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.

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.

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

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

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0