Ask HN: Interest in a Rust-Inspired Language Compiling to JavaScript?
I'm considering the development of a new programming language, drawing inspiration from Rust's strengths, with a focus on compiling to JavaScript. Here what I'm considering are some key features:
- Strict Typing System: no type-related bugs.
- Algebraic Data Types.
- Exhaustiveness Checks.
- 'Unsafe' Mode: Directly interact with external JavaScript/TypeScript.
- no 'null/undefined': Option/Result instead.
- Trait-Based Programming.
- Backend Development Focus.
- Compiler: dead-code elimination and partial evaluation, similar to Prepack[0] (by Facebook).
I believe this approach could bring significant benefits, especially with recent additions like TypedArray and worker threads.
Would this be of interest to the community? Looking forward to your insights and discussion.
Currently I don't feel confident to complete the project as I am so any recommended resources[1] would be extremely helpful, especially around the area of implementing types.
[0] https://github.com/facebookarchive/prepack
[1] https://github.com/taosx/axido (nothing is decided)
26 comments
[ 2.8 ms ] story [ 79.6 ms ] thread[0] https://www.roc-lang.org/
I HATE most js frameworks for this reason. Understanding/Tweaking websites is something I do extensively, and obfuscated minified recursive js makes this REALLY hard.
I don't like how low level Lit components are, but one thing I do appreciate about them is how little there is getting in the way of using a debugger, which is probably my biggest complaint with reactive frameworks.
You mention a focus on backend development. Why suffer having to compile to JS if you are not restricted by the runtime environment? (in the backend, surely you can run anything). In my understanding, compiling to JS is only interesting when you want to run in the browser, but outside this constraint, JS seems like a less than ideal compilation target (though probably way better than before thanks to the presence of typed arrays).
Is it interoperability with the JS ecosystem? Wouldn't the no null/undefined criterion be a barrier to this? Or will you take any result from a call to a JS function as optional? Or have interfaces saying "I promise, I will never return null/undefined"?
Or you still want your language to be able to run in browsers? In this case, will you provide a standard library (with the cost of having to transfer it every time it's used), or only a thin wrapper around native JS functions?
You say Rust-inspired. For me, one of the most interesting features of Rust is the explicit management of the lifetime of objects. How do you intend to handle this aspect, with JS not having anything like this?
Browser, Std lib: Yes, the language should still be able to run in the browser and will provide a std but will not transfer each time, the benefit of writing a compiler would be that we can do dead-code-elimination and target native javascript methods instead of something like wasm.
Lifetimes: Honestly, currently I'm only working on the simplest transpiler but I would like to explore using typed arrays as the stack, heap.
Good luck in your project and have a lot of fun.
What's an imagined use case, here?
My recommendation is to create a new language that’s focused exclusively on functions and how to organize them. Organization is an advanced skill learned from experience that is not immediately benefitted from high intelligence and most developers fail in that regard. Think in terms of lexical structures, events, return types, data structures, process flow (technical and business), and call stack.
If you can really dominate concepts of organization by language design the best supporting syntax innovations will shift to more specifically meet your needs.
One of the additional goals I have is to be able to write type safe APIs between frontend and backend similar to trpc (elysia framework).
In my experience writing large applications in JavaScript the front end (in the browser) and the back end are less different than people typically consider. It could be that many people struggle with the DOM, but anyways it’s all APIs and event handling.
> It could be that many people struggle with the DOM, but anyways it’s all APIs and event handling.
Without a framework, how do you usually like to handle cross-component/cross-screen state in the frontend? Like where do you keep track of logged-in state, dark mode, the shopping cart, etc? Do you just like to put that in the backend, or is there a neat way to organize such data in vanilla JS in the frontend?
Well typed, I'd imagine the dev ex would be similar to a monolithic React context. Seems like it'd work.
Just doing types, classes, interfaces again and again isn't really interesting.
Most interesting thing in Rust is not the type system or borrow checker but value semantics.
For what it takes, it can inspire you and I find the language very expressive and flexible.
[0] : https://nim-lang.org/
I'm not so sure about implementation of Rust's semantics in JS. If I want exact Rust semantics, I can use WASM.
From recent experiences in profiling and optimizing different backend apps, I'm confident that imposing some constraints on JS we can get noteworthy improvements on the performance front better or equal to golang. Here's a subset of the constraints I'm looking at:
- Avoid out-of-bound reads in arrays and monomorphization.
- Manage memory allocations. (pools)
- Implementing inlining, also done by V8 and JSCore.
- Keep variables close to usage context.
- Being picky about standard js operations.
- Using 'new Array(n)' for creating longer arrays, and '.push()' for smaller ones but if frequently calling methods on an array, avoid preallocating.
- Avoid arrays with holes, prefer using arrays with numbers.
- Use cached prototypes for object creation.
- Avoid working with strings as much as possible, use typed array when possible.
- Partial evaluation.
ps: Thank you for all your work in the Rust ecosystem, I've used a lot of your crates over the last year.
It's aimed at full stack programming, not exclusively backend.
However, modern JS-oriented toolchain for ReasonML is called ReScript and you can learn more here: https://rescript-lang.org/
Please stop right here.
Real world is not a theorem.
We, down to earth developers, have had enough of this over engineered crap. We need flexibility.
There is already a near perfect language for JS.
It’s called Typescript.
Youd make a much more good for the world if you improve it, fix few issues etc.
Another theoretical fantasy that has nothing to do with real world.
In real world we do have null and undefined values. It is artificial and redundant to pretend that there isn’t.