For a while, I've had a vision for my "dream" language. ((Sorry @mortoray , I don't think it's Leaf, still love your content))
The syntax I've come up with is inspired by JavaScript but with some added flair that would make it (best case scenario) compatible with ASM and WASM compilation.
I've left some sample code below, and I was wondering if anyone knew if a language similar to the snippet below exists.
import { sqrt, pow } from "math"
export class Point {
struct {
Int x,
Int y
}
constructor(Int a, Int b) {
this.x = a;
this.y = b;
}
constructor(Int a) {
this(a, a);
}
constructor() {
this(0, 0);
}
distanceTo(Point pt) -> Float {
return sqrt(pow(pt.x - this.x, 2) + pow(pt.y - this.y, 2));
}
}
export class Circle {
struct {
Point center,
Int radius
}
constructor(Point c, Int r) {
this.center = c;
this.radius = r;
}
constructor(Int r) {
this(new Point(0, 0), r);
}
constructor() {
this(1);
}
intersects(Point pt) -> Boolean {
return pt.distanceTo(this.center) <= this.radius;
}
}
Thanks in advance for any help!
In hindsight this looks a lot like a real Java-Script like syntax but I was still curious if anyone knew if this existed before I went out and tried to start making a compiler for a project that's already been worked on :)
Top comments (18)
TypeScript seems closest. There are other static type checkers like flow.
Flow is much like that, yeah. Variable declaration is slightly different, but overall, at first I thought the example was ES2015. 😀
Your dream language has semi-colons!? :P
The challenge of any language, as I'm learning, is the addition of more features. In isolation one can come up with pretty syntax. Thrown into an app where a coder wants to do a lot, ugliness starts to appear.
Having examples of what people like and dislike though is an important process in language design.
I find it confusing that the types come before the name for variables and after the name for functions...
did you make the illustration by hands? or there is some tools can do this
Just use a monospace font - any programming-oriented text editor will do.
That looks like the return value, similar to how it's done in rust, what goes in the function is on the left, what comes out of the function is on the right.
In Rust the type is on the right in both variables and the return:
In C, the type is on the left for both the variables and the function (though one can argue that the arguments are also part of the function's type):
In this syntax the order is sometimes type...name and sometimes name...type:
AH, yes you're right, I missed what you were specifically talking about.
The type is on the left when it's required and on the right when it's optional
Wren might be getting there? It's not 1:1, of course, and it's not "for the web".
As a few other folks have mentioned, Typescript indeed also sounds close to what you want, especially if you're looking for interoperability with the JS ecosystem.
I don't know if such a language exists but writing an interpreter for it should be fairly easy. You can modify an existing Javascript interpreter or use Pegjs to generate a parser.
Seems close to Dart, especially in strong mode.
Looks a lot like es6 but then if you wanted something smoother then typescript
Looks like Kotlin, Dart and lots of other OO languages.
Swift gets close.
This is basically every statically typed OOP language ever. Btw no idea on semantics? Really? I mean syntax is probably one of tge least inportant aspects in a programming language...