Erlang's pattern matching
Erlang's arity
Make browser run on Erlang's VM
Compile javascript to BEAM
Eventually get rid of javascript
World is is a happy place now
return false if x == 10
Basically the condition follows the expressions, this can help readability in many scenarios.
Let's take this code for example:
function doubleSay (str) {
return str + ", " + str;
}
function capitalize (str) {
return str[0].toUpperCase() + str.substring(1);
}
function exclaim (str) {
return str + '!';
}
If we want to call the functions in a precise way we would do something like:
const result = exclaim(capitalize(doubleSay("hello")));
With a pipe operator, the above code will look like this:
const result = "hello"
|> doubleSay
|> capitalize
|> exclaim;
It's way more readable and understandable.
There is a proposal already for adding a pipe operator in ECMAScript - github.com/tc39/proposal-pipeline-operator. Let's hope it gets approved and moved in the higher stages soon.
JavaScript stores numbers as double precision floating point numbers, following the international IEEE 754 standard. This leads to strange behaviours when working with numbers - just try to sum 0.1 and 0.2 and see the result.
With the rise of TypeScript and Flow, developers start to appreciate the need for static types, especially when we talk about big JavaScript projects.
ECMAScript 4 had a type system, but the standard was abandoned, due to political differences concerning language complexity.
There is a proposal for an optional type system which is in State 0.
We have const , which some see as an immutable data structure, but that is not the case. const provides an immutable binding, not pure immutability. Take the following example:
const names = ['Hashnode', 'Google', 'Facebook'];
names[3] = 'Twitter';
console.log(names) // ['Hashnode', 'Google', 'Facebook', 'Twitter']
As you can see, the array can be changed, even when assigned to a const variable type.
A true immutable object would throw an error when someone tries to change not only the value of the variable, but the members too.
This can also lead to a native deepEqual method.
Heh, why "given a chance"? I already do so!
For example: Result and Option from Rust (and get rid of undefined, null, NaN, etc.)
const Result = require('result-js');
Result.registerGlobals();
function foo() {
const e = new Error('don\'t throw, return ;)');
e.errno = 'ESOMETHING';
e.code = 42;
return Err(e);
}
const result = foo().orUnwrap('use default value, drop error');
Also, I would snatch Type declarations (also called Interfaces or Abstract Classes in other languages) from Object-Pascal, if not for my way of architecturing modules, which already ends up with something very similar.
In addition to that, I would love to have mix-able objects, so that I could compose objects instead of using prototype-inheritance... Though I use mics for that at the moment, which works pretty well!
// might work like that, when implemented into language:
const foo = { work: ()=>{ console.log('foo.work'); } };
const bar = { work: ()=>{ console.log('bar.work'); } };
const fooBar = Object.mix(foo, bar);
fooBar.work();
// Outputs:
// foo.work
// bar.work
On top of that, a more expression-oriented style (again taken from Rust)... The only hack-around is to use functions, which are called in-place. That's a little unperformant for such a simple assignment imho.
const myVar = switch (foo) {
case 'foo': break 1;
case 'bar': break 2;
default: break 3;
};
// or
let i = 0;
const someVar = while (i < 3) {
if (i % 2) {
break i;
}
i++;
};
// instead of
const otherVar = (() => {
switch (foo) {
case 'foo': return 1;
case 'bar': return 2;
default: return 3;
};
})();
Aaaand, I am missing default-immutability of variable values (as opposed to immutability of variable references, which is implemented with const), again taken from Rust (yeah, I love Rust!):
fn main() {
let i = 0;
i = 1; // ERROR! i is not mutable
let mut j = 0;
j = 1; // OK
}
Might be implemented into JS like this:
function main() {
leti i = 0;
i = 1; // ERROR! i is not mutable
consti j = { foo: 0 };
j.foo = 1; // ERROR! j is not mutable
}
My points are:
I personally like contracts something like "i is int and i > 0" so i can reduce the amount of checks inside of the function and concentrate on what's relevant.
Suz Hinton
Node hardware developer, web + cloud enthusiast, and accessibility advocate
proper date handling 🙈