erights / Orthogonal-Classes

Proposed EcmaScript Class Syntax clarifying orthogonal concerns
29 stars 2 forks source link

re: "Class scoped lexical function declarations" #16

Open matthewrobb opened 7 years ago

matthewrobb commented 7 years ago

Class scoped lexical function declarations

If my mind is correctly understanding this I would just like to express that I think this is a wonderful idea.

It also brought to mind the idea of instance-bound methods which currently if using a transpiler like babel you would express like this:

class Foo {
  bar = ()=> {}
}

But this has limitations for example I don't believe you can perform super calls from within them. It may have been suggested before but it seems as though an easy win improvement could be:

class Foo {
  bar()=> {}
}

The only syntactic difference in my mind would be strictly enforcing the use of parens but I could also see a valid argument for strictly enforcing a block body.

allenwb commented 7 years ago

what we have in mind is using actual lexical declarations within the nested class scope:

class Foo {
   function bar() {};  //a function declaration, lexically scope to class body.
   const k = 42;          //a const lexically scoped to class body
   const sum = (a,b) => a+b;
   let count = 0;    //a variable scoped to the class body
   class FooHelper {
      own a,b;
      constructor(a) {
          count++;
          this.a=a;
       }
   }
   own #x = new FooHelp(1);  //private field initialized to lexically protected helper class
}

Note that the bindings created by such lexical declarations are not fields or properties of any of the constructor, prototype, or instance objects associated with the class declaration. They are ordinary lexical bindings whose scope is the class body.