I noted some open issues on "Classes with Trait Composition"

Bob Nystrom rnystrom at google.com
Wed May 18 10:58:36 PDT 2011


On Mon, May 16, 2011 at 8:02 AM, Brendan Eich <brendan at mozilla.com> wrote:

> On May 16, 2011, at 4:54 AM, Dmitry A. Soshnikov wrote:
>
> Regarding `new` keyword for the constructor (aka initializer), after all,
> it als may be OK. E.g. Ruby uses `new` as exactly the method of a class --
> Array.new, Object.new, etc. Though,  `constructor` is also good yeah.
>
>
> My point is not to bikeshed, rather (a) to name existing prototype
> properties minimally, (b) to avoid preempting other names.
>

Using "new" for the constructor is one of my favorite feature's of Allen's
proposal. Things I like about it:

1. It's terse. Since almost every class defines a ctor, this is helpful.
"constructor" is a mouthful and repeating the full class name (like in Java,
C++, etc.) is redundant.
2. I think it's pretty clear to a user what's going on.
3. It avoids stealing a valid identifier. You can't define a method named
"new" but "constructor" is a valid property name.


> Good, bad, or in between, the prototypal pattern in JS for constructor C
> binds C.prototype.constructorr to C. It does not bind C.prototype.new.
>

That's true, but I hope for classes is to hide just that machinery. There's
no mention of "prototype" when you define a property on that object inside a
class but that's what happens under the hood.

My hope is that users should almost never need to know or care that
C.prototype.constructor is bound to C. Instead, what I'd like is for "class"
to be a valid expression inside a class member that evaluates to C. Instead
of:

class math.geometry.Circle {
  class var pi = 3;
  new(radius) {
    this.radius = radius;
  }

  get circumference() {
    return this.radius * 2 * math.geometry.Circle.pi;
  }
}

Be able to do:

class math.geometry.Circle {
  class var pi = 3;
  new(radius) {
    this.radius = radius;
  }

  get circumference() {
    return this.radius * 2 * class.pi;  // <---
  }
}

- bob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110518/828fc7e0/attachment-0001.html>


More information about the es-discuss mailing list