欢迎来到编程语言的未来:OK?
Try it out on the playground
Watch the Making-of video
OK?'s mascot: Quentyn Questionmark.
OK? is a modern, dynamically typed programming language with a vision for the future. OK?'s mission is to do away with the needless complexity of today's programming languages and let you focus on what matters: writing code that makes a difference.
Very early on in its design, it was decided that OK? would not feature a ternary operator. For those unaware, the ternary operator looks like this:
let a = isprod ? 'prod' : 'dev';Disgusting, we agree. Einstein, Tesla, and Newton all died long ago, so there's really only a handful of humans left on Earth who are capable of parsing that stupifying syntax. What does the question mark mean? What does the colon mean? In OK? we leave those questions for the philosophers and focus on what's important: writing clean code.
A language only needs one conditional control flow construct, and in OK?, that construct is the switch statement. Switch statements are more versatile and expressive than ternary operators and if statements, and after a while you'll forget those other constructs ever existed. Here's the above statement in idiomatic OK?:
let a = switch isprod {
case true: "prod";
case false: "dev";
}The switch form, although longer, is unquestionably clearer. Understanding the value of simplicity over complexity is the first step to learning OK?
Given that switches are so central to OK?, we wanted to avoid some common pitfalls around switches found in other languages. In other languages it's common to have a single switch statement take up several pages of an editor with bloated logic being shoved into each switch case, so in OK? we made it that you can only have one statement per case:
If you want to execute multiple statements per switch case, just wrap them in a function:
// INVALID:
switch x {
case true:
z = z + 2
case false:
x = x + 1;
y = y - 1; // = reqcreds && p.nottype("Admin") {
...
}I think I pulled a neck muscle trying to read that obscenely long line.
In OK?, the && and || operators can only act on variables, so that it's possible for the reader to understand what's going on.
// ERROR: '&&' operator must act on variables
switch p.isactive() && p.credits() >= reqcreds && p.nottype("Admin") {
...
}Here's how the above switch statement would be done in OK?
let isactive = p.isactive()
let enoughcr = p.credits() >= reqcreds
let notadmin = p.nottype("Admin")
switch isactive && enoughcr && notadmin {
...
}This reflects a central tenet of OK?: be kind to the reader.
If you need to short-circuit your conditionals, you can use the lazy keyword:
let isactive = p.isactive()
let enoughcr = lazy p.credits() >= reqcredits
let notadmin = lazy p.nottype("Admin")
switch isactive && enoughcr && notadmin {
...
}If p.isactive() returns false, then p.credits() and p.usertype() will never be called.
With this feature you get the best of both worlds: clean, readable code, without sacrificing performance.
Ever found yourself running in circles looking for the right comparison operator? Instead of wasting time memorising the hieroglyphics of ==, !=, >, =, and =
| In other languages | In OK? |
|---|---|
a >= b |
a >= b |
a = a |
|
a > b |
!(b >= a) |
a = b) |
|
a == b |
let x = a >= b; let y = b >= a; x && y |
a != b |
let x = !(a >= b); let y = !(b >= a); x || y |
This means that instead of juggling six different comparison operators in your head you can focus on what matters: creating great software.
In the OK? standard library we haven't found the need to extract any of these expressions into their own functions, but there's nothing stopping you from rolling your own:
let equals = fn(a, b) {
let x = a >= b;
let y = lazy b >= a;
return x && y;
}in OK?, 5 + 2 * 3 evaluates to 21, not 11, because addition and multiplication have equal operator precedence. If you want to evaluate your expression in some other order, you simply need to use parentheses: 5 + (2 * 3).
This simple left-to-right default spares you from scrounging around the internet looking for an operator precedence table, and lets you keep your eyes on the code.
The authors of OK? watched as object-oriented (OO) languages boomed in popularity, only to find them soon buckling under their own weight. Central to this clinical obesity is the class.
A class takes a sensible idea: defining data along with methods that act on that data, and then drives it off a cliff by adding inheritance and subtype polymorphism. It should be no surprise that a bunch of class-obsessed aristocratic oldies in the 60s, who probably spent all their time deciding which child should inherit most of the estate, decided to add a construct named 'class' which revolved around inheritance.
Well, the revolution has finally come! There is no inheritance in OK?, in solidarity with all those who fought against the bourgeoisie in years past. Some other languages opted for structs over classes yet despite their developers honourably denouncing OO, old habits die hard with some accidentally uttering 'class' when they mean 'struct'. Some developers even say 'class' deliberately, a dogwhistle to return to the old days when the OO aristocracy still held the developer profession by the throat.
To remove any ambiguity and to ensure full commitment to the death of classes, we've decided to use our own terminology: 'notaclass' , or 'nac' for short.
notaclass person {
field name
field email
}Let's deep dive into what makes our nacs special:
You don't check how your friend is feeling by prying them open with a crowbar and perusing through their entrails; you just ask them. The same is true in programming. There is no way to mark a field as public in OK? because the public API of a nac should describe behaviour, not state.
let p = new person()
p.name = "Jesse" // = 2 }); // falseWith this speed, your program's going to finish before you've even started writing it.
Dave says:
OK? has transformed me from an angry developer to a happy developer. I used to think composition over inheritance, having my own projects composed of various different languages, never thinking about the power of evolution. Now I've evolved to only use one language: OK?
Joel says:
I used to find OK?'s opinionated syntax constraints coercive. Now I find them liberating. Thinking hard about how to fit a complex variable name into eight characters forces me to write code that future me can easily maintain
Sarah says:
I used to think there were all these features I needed to write quality software, but after a while working with OK? it just clicked: when you stick to the basics, the resulting code is clear and easy to understand.
Jack says:
If I'm on the OK? subreddit and I see you use the word 'class' when you meant to say 'nac', I'm going to start screaming. And that screaming will not stop until I've found you. By then, you'll be the one screaming.
To play around with the language in your browser, you can go to the playground
To use the language locally, follow the following steps:
git clone the repo.ok directory run go install.ok without any arguments to bring up the REPL, or you can run an OK? file with ok test.ok.Happy OK'ing!
暂无开放 Issues,或尚未同步最近议题。