-
Notifications
You must be signed in to change notification settings - Fork 3
AWB alternative proposal sketch #1 #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Sounds like our two proposals have a lot in common, which is great! A semantic question: what happens if I don't "initialize" an instance variable before the class constructor returns? class Point {
private x, y;
constructor(x,y) {
this..x = x;
// I forgot to initialize "y"
}
} |
In my proposal, Note, that this means that the shape of an object's instance var state is not (easily) determined before or at object allocation time. This used to be a requirement but my understanding in that the current class fields proposal dropped that requirement so I assume that we can also ignore it as a requirement. Also, in my proposal there is no particular reason that a |
I think so (and I like the easy friendship patterns), but there's one issue that I'm curious about. Suppose we have the following: class C {
private x;
constructor(x) { this..x = x }
get x() { return this..x }
doAssignment() { this..x = 12345 }
} And I do this: let obj = {};
// Attach C prototype methods to this object
Object.defineProperties(obj, Object.getOwnPropertyDescriptors(C.prototype));
obj.doAssignment();
console.log(obj.x); // 12345? It would seem to be the case that I have an object |
Good point. With dynamically added instance variables you can't ways depend upon using the existence of an instance variable as a brand. BTW, I think what you show is probably a fine use case for inst vars, it just isn't branded and/or sealed. What you probably would have to do is make sure that you do your branding setup in the constructor. So one way: class C {
private x, brand;
constructor(x) {
this..brand = true;
this..x = x }
get x() {return this..brand, this..x } //assumes accessing a missing instance var throws
doAssignment() { this..brand, this..x = 12345 }
} or assuming that Object.seal prevents adding additional instance vars: class C {
private x;
constructor(x) {
this..x = x;
Object.seal(this)}
get x() {return this..x } //assumes accessing a missing instance var throws
doAssignment() { this..x = 12345 } // assumes storing into a missing instance var throws
} But, sealing in the constructor is problematic if you might subclass it. I have a possible solution to that problem, but I was going to save it for my classes 1.2 addendum. |
How would you rewrite "private methods" using this proposal? My guess is that the options are:
|
alternatively, Declare lexically scoped variables outside of the class and class body static initializer block to initialize those variables using function expressions that can reference instvar keys declared within class body.
sure, but I typically wouldn't want to do this because I don't want to add redundent inst var state to each instance. Any of these technique would work with my classes 1.1 proposal. But, they are admittedly kind of ugly. That is why my classes 1.2 proposal would add class body lexical function (at least) declaration possibly a syntactic extension to avoid having to invoke them as |
Reminds me of the presentation I gave in Paris, using the old It looks like all the momentum is behind the pipeline operator for chaining, but that might work for this proposal as well: class C {
private .foo;
function init(instance) {
return instance..foo = '123';
}
constructor() {
this |> init;
}
} Going back a bit, this discussion has clarified for me a constraint that we've assumed in the past. In previous proposals, we've assumed a distinction between adding an instance variable to an object and setting an instance variable. A "field" declaration has been a convenient way to express that distinction. |
Closing in preference to #7 |
Uh oh!
There was an error while loading. Please reload this page.
I found it very useful to consider you assumptions/constraints. However, some I agree with; others not so much. I think it will be useful to talk about them relative to another stack-in-the-ground proposal. So here is my first cut at a sketch of one.
Max-min classes 1.1. AWB alternative sketch 1
Basic Concepts
private
declarations introduce new instance variable keys into a parallel scope (does not shadow non-private name)Example
The text was updated successfully, but these errors were encountered: