by auggierose · 4y ago · 0 comments
That is an awesome goal. I could use it right now! It would be great if features like the Or type A | B from TypeScript and value types could be preserved though. But that will be difficult to do.
There are projects like Reason and ReScript that do something similar with JavaScript as a target, but these projects don't seem to be mature / reliable enough to base a large project on.
But if you are going for it, that is great. Here are some features I would love for a language like derw. This is just meant to be a source of ideas you may or may not have thought of already, and obviously, your goals probably differ from mine:
1. Optimised arithmetic for 32-bit integer types int and uint.
2. Tagged unions based on uint, compiling to "const enum".
3. Provide an out-of-the box ! operator that actually throws an exception if the value is null or undefined.
4. Support for both mutable and immutable data structures out of the box.
5. Support for building reliable APIs, that cannot be breached even at runtime through some meddling JavaScript code. Currently I use Object.freeze all over the place, just to make sure that people cannot change randomly a class definition from right under me, etc. For example I have a decorator @fixed that is defined as
export function freeze<V>(x : V) : V {
if (typeof x === "function") {
Object.freeze(x.prototype);
Object.freeze(x);
} else {
Object.freeze(x);
}
return x;
}
freeze(freeze);
export function fixed<T extends { new (...args: any[]): object }>(target: T): T {
return freeze(class Final extends target {
constructor(...args: any[]) {
if (new.target !== Final) {
throw new Error('Cannot inherit from immutable class');
}
super(...args);
freeze(this);
}
});
}
freeze(fixed);The ReScript creators have pretty good experience I believe, building their own compiler based on Reason and OCaml.
I'm good friends with his sister :)