-
Notifications
You must be signed in to change notification settings - Fork 3
Closed
Description
Max-min classes 1.1. AWB alternative 2
An updated version of this proposal is below at #7 (comment)
Here is a proposal that is closer to the @zenparsing parsing original, but perhaps simpler. With this proposal, I don't necessarily see the need for a classes 1.2 follow-on
Basic Concepts
- This proposal adds the concept of secret class elements to ECMAScript class definitions. Secret class elements are only accessabe from within the body of a class definition.
- There are three kinds of secret class element, static initializer blocks, instance variable definitions, and secret method definitions.
- A class definition may contain at most one static initializer block it is automatically executed as the final step of class def evaluation
- The static initializer block is a secret of the class definition, there is no way to invoke it from outside of the class definition
- A instance variable definition defines one or more secret variables that exist in each instance of a class.
- An instance variable defnition within a class body looks like this:
var x,y; //each instance created by this class constructor will have these two instance variables
- Within a class body, instance variables are accessed using the -> operator like this:
this->x = 0; console.log(obj->y);
- Instances variable names are lexically scoped and visible to everything contained in a class body.
- Attempting to access an instance variable using -> produces a runtime ReferenceError if the object does not have the specific named instance variable defined by this class definition. In other words, A reference to an instance variable only works when the objectise a normally constructed instance of this class or one of its subclasses.
- Instance variable definitions don't have initializers. Instance variables are usually initialized by the constructor. An uninitialized instance variable has the value
undefined.
- A secret method definition is a concise method that can only be directly involked from inside the class body.
- A secret method definition looks exactly like a concise method except its name is prefixed by
-> - Here are some examples of secret method definitions
->foo() {return super.bar()} get ->x() {return this->_x} //a secret accessor method set ->x(v) {this->_x = v}
- Secret method names are lexically scoped and visible to everything contained in a class body.
- Within a class body, secret methods are invoked using the -> operator like this:
this->foo(); obj->x = 5; //invokes a secret set accessor method this->foo.bind(this); //access the function value of a secret method
- Secret methods can be accessed/invoked with any object supplied to the left of the
->. It is up to the code of the method body whether or not a runtime error will occur if thethisvalue is not an instance of the containing class or one of its subclasses. Access to instance variables that don't exist for thethisvalue would, of course, throw.
- A secret method definition looks exactly like a concise method except its name is prefixed by
- It is an early error if the name occuring to the right of a
->operator is not the lexically visible name of a secret method. - No reflection on instance variables, instance variable names, secret methods, or secret method names
- Lexically scoped instance variable and secret method declarations introduce new names into a parallel scope (does not shadow non-secret names)
Example
class Point {
var x; //or: var x, y;
var y;
->brandcheck() {
try {this->x} catch {return false};
return true;
}
get x() {return this->x} //normal accessor properties
get y() {return this->y}
add(another) { //a normal method property
if (another.brandcheck())
return new Point(this->x+another->x, this->y+another->y);
else
return new Point(this->x+another.x, this->y+another.y);
}
constructor (x,y) {
this->x = x;
this->y = y;
}
}Technical Notes and Rationale
varis repurposed to declare instance variables because its name is suggestive of "instance VARiable". Note that several early ES class proposals including the original ES4 proposals usedvarfor this purpose.- Secret names (instance variables and secret methods) exit in the same namespace so an instance variable may not have the same name as a secret method.
- The kind of thing a secret name is bound to is based upon its declration. -> may statically determines from the supplied name whether it is accessing an instance variable, a secret accessor method, or invoking a secret method.
- Any concise method form may be used to define a secret method. It's the -> prefix to the name that makes it a secret method.
- Secret methods don't have any special semantics for their bodies. The Home object of a secret method is the prototype object defined by the class.
- There is no particular reason why we couldn't have
staticinstance variables andstaticsecret method definitions. But it is simplier to not have to explain how they differ from the non-static forms. - Instance variable declrations do not have initializers to simplify things. Without them, we don't have to define their evaluaiton order or the scoping of the initializer expressions. People are already used to using constructors to initialize instance state.
- It is an early error to assign to a secret method reference (unless it is an accessor method)
- Because secret methods are statically resolvable and immutable, it is easy for an implementation to statically in-line them. No flow analysis or dynamic specialization required.
- The
vardeclarations of a class collective and statically defined the "shape` of the secret instance state introduced by that class definition. Subclass can add additional secret state "shapes" but overall it is a step closer to objects with a fixed shape determined at class definition time and should also make it easier to lift the internal checks needed on instance var accesses.
Metadata
Metadata
Assignees
Labels
No labels