Ticket #166 (new defect)

Opened 1 year ago

Last modified 6 months ago

(Resolved) Private constructors are useful, common, and need to be supported

Reported by: lth Assigned to: lth
Type: defect Priority: trivial
Milestone: Component: Spec
Version: 4 Keywords:
Cc: jeffdyer,brendan,graydon

Description

We do not have private/protected/namespaced constructors. This neglects many use cases, at least singleton classes, abstract classes, and classes whose instances must be constructed in various controlled ways, for example, through facades that supply various kinds of information for convenience that user code can't easily compute.

IMO we need to fix this. The ickyness of how the Math object is constructed (by hiding its real type -- until meta-objects are working!) shows too well how poorly this works.

Attachments

Change History

Changed 1 year ago by lth

  • owner deleted
  • component changed from Proposals to Spec

(1) The constructor can be annotated with "private", "protected", "public", "internal" namespaces. (2) The "new" operator must check that the constructor is accessible to the calling code

Note, there are injection attacks where an accessible class with an inaccessible constructor can be passed to a class-internal function that performs the construction for it, by accident.

Three solutions on the table:

  • (1) magic constructor function/new syntax
  • (2) class must be no more accessible than constructor
  • (3) if a constructor is annotated then "new" must name the class directly

Solution (3) is favored for the time being.

Changed 1 year ago by lth

Open issues for solution (3):

  • What does it mean for a constructor to be "annotated"? Does "use default namespace ns" count?
  • General agreement that anything but "public" applies.
  • Run-time or compile-time error when "new" is applied to class with annotated constructor if class not named directly in "new" expression.

Changed 1 year ago by lth

  • owner set to lth
  • priority changed from major to trivial
  • summary changed from Private constructors are useful, common, and need to be supported to (Resolved) Private constructors are useful, common, and need to be supported

Changed 6 months ago by airforce1

Note: See TracTickets for help on using tickets.