- JS++ by itself
In a similar spirit, one of the key innovations behind JS++ is “type guarantees.” Whereas we’ve previously had to content ourselves with type checking, as engineers, we want guarantees: a guarantee the variables you declare as strings don’t become numbers, a guarantee the variables you declare as numbers don’t become objects, and a guarantee your class instances don’t become strings at runtime. This is what is known as type preservation, a byproduct of a “sound” type system.
Type guarantees: Beyond type checking
To illustrate this point, let’s examine a basic variable declaration:
bool x = y;
This typically compiles to:
var x = y;
Fortunately, there are solutions. Slightly more sophisticated systems use “runtime contracts”:
assert(typeof y == “boolean”);
var x = y;
Runtime contracts can ensure types are correct at runtime. However, they have a glaring weakness: They take down the entire application on a single error. With runtime contracts, you risk having exponential points of failure. In software engineering, we want to reduce the potential points of failure.
We’ve worked on the JS++ type system since 2011. We explored every type system under the sun: Hindley-Milner type inference, gradual typing, soft typing, and more. The JS++ type system is optional and sound. In other words, you do not need to declare types for your variables and functions. Type annotations are completely optional. However, if you do decide to declare the type for a variable, it is guaranteed to be correct at both compile and runtime. This is the breakthrough in JS++, as previous systems could only achieve correctness at compile time or runtime, but never both.
In JS++, types are guaranteed. At compile time, the type system is “sound”—in other words, the JS++ compiler’s type checker can never be wrong because it’s not approximating. As an example, if the JS++ type checker determines at compile time that an expression will evaluate to Boolean, it will evaluate to Boolean at runtime. This is aided by runtime “enforcement.”
Runtime type enforcement is lightweight and straightforward. When we declare a Boolean, the compiler ensures the data always remains a Boolean via conversions:
var x = !!y; // Convert ‘y’ to Boolean via double !! operators
Conversions on primitive data types like Booleans and strings are implicit, automatic and straightforward. However, another challenge we faced in our design was how we could guarantee the types to be correct for constructed types. For instance, one “Employee” class within an organization may differ from the “Employee” class defined for another organization. In this case, JS++ enables developers to define custom conversion rules and logic.
Conversions are lightweight. They only need to be performed on variable assignments, function calls and function returns. If a conversion fails, you simply get the default value for the type, such as an empty string for the “string” data type. Furthermore, the compiler can optimize conversions so that they only occur at runtime when necessary. Thus, you get the benefits of type safety without sacrificing performance.
The example was oversimplified, and there’s a lot more science behind the JS++ type system, but this highlights one of the main innovations behind JS++: type guarantees. JS++ goes beyond type checking and guarantees the types you declare will be correct at both compile and runtime.
Object-oriented programming with classes
Structure is how we know an Employee class has a getName method. Structure is how we determine that getName was a typo that could crash the program by calling an undefined method. Structure is how the linker knows which modules were unused so we can perform dead code elimination.
The end of the prototype debate is that real-world systems ranging from banking to rocketry are built with classes. Maybe prototypes enable greater “expressiveness.” Maybe prototypes enable greater “composability.” However, no amount of fear mongering can refute the fact that classes are working and working well. Lest my statements be taken out of context, JS++ was not designed for rocketry; however, if its underlying principles can be used to explore the cosmos, it can certainly be used to ship higher-quality web and mobile applications.
The little details
var x : boolean = true;
bool x = true;
Additionally, the JS++ compiler is written in C++. As projects grow in complexity, we think developers will appreciate the extra engineering effort we invested in order to achieve the fastest compilation times. Compilation times are only one aspect of programming language performance. Runtime performance is arguably even more important, and we’re working on this too. Maximum speed is important.
The little details add up over time. JS++ wants you to have the smoothest development experience possible.
There has been some concern over my company, Onux, filing patents on the JS++ type system. The patent filings are a defensive mechanism. We don’t charge royalties for using JS++. We’ve sunk a lot of resources into R&D, and the result is the most reliable type system in the industry being provided to you for free. Lost in the chaos is that some of the most popular programming languages are patented. Microsoft’s C# is heavily fortified with patents but remains unencumbered.
I can’t speak for other programming languages, but, typically, when patents are used defensively to protect R&D investments, it’s used to stop competitors from copying rather than tormenting users. The JS++ compiler is free to download for a reason. Nevertheless, for the remaining skeptics, we think having type “guarantees” over type “checking” will interest early adopters and developers that value our same values and rock-solid reliability.
When you really begin to dig deep into the problem, you realize that the JS++ type system works in virtually every case. This takes experience, this takes time, and this takes effort. We’ve worked hard to come up with an innovative solution. When people ask us to lift our patents and make it open source, they are asking us to forfeit years of hard work to Microsoft et al.
It’s never too late for us to pivot on our patent stance because we understand it’s unpopular, but, presently, we are choosing to remain pragmatic even if it costs us die-hard anti-patent purists. JS++ was only just announced and is still in the developer preview stage, so a lot can change over time. However, Onux is not and will not be a freebie research arm for Microsoft, Google and Facebook. They have the resources and bright minds to find another way to achieve type guarantees.