Replies: 2 comments 3 replies
-
Do you know about TypeScript Constructor Assignments? Basically, you can: class Person {
constructor(public name: string, private last: str) { }
} And the properties will be assigned as either public or private to the class. |
Beta Was this translation helpful? Give feedback.
-
Here's a more elaborate example based on some existing code in the winglibs repo: pub class Library {
extern "./util.js" static sortedArray(arr: Array<str>): Array<str>;
new(workflowdir: str, libdir: str) {
let this.dir = libdir;
let this.name = fs.basename(libdir); // <--
let pkgjsonpath = "{libdir}/package.json";
let pkgjson = fs.readJson(pkgjsonpath);
let this.manifest = PackageManifest.fromJson(pkgjson); // <--
log(this.manifest.name);
let this.buildJob = "build-{this.name}"; // <--
let expected = "@winglibs/{this.name}";
if this.manifest.name != expected {
throw "'name' in {pkgjsonpath} is expected to be {expected}";
}
let this.platforms = Library.sortedArray(this.manifest.wing?.platforms ?? []);
if this.platforms.length == 0 {
throw "\"{this.name}\" winglib does not have a `wing.platforms` field in its package.json.";
}
// ...
}
} The syntax is interesting theoretically. One thing nice about it is that if you want to go to the definition of a field like To me the only risk of this syntax is that it allows the field's type to be omitted. Like with method signatures, I think public APIs such as fields should be strongly-typed (specified by the user, not implicitly determined based on the body of the function). But if Once we add getters and setters it should be easier to add this kind of sugar safely |
Beta Was this translation helpful? Give feedback.
-
Instead of the unbelievable annoying syntax for field declarations in classes:
In order to define public fields, we should use getters:
And we can add some sugar to this. Maybe:
Beta Was this translation helpful? Give feedback.
All reactions