Sunday, September 6, 2015

js backend: typed maps


There was alot of hype on Dart not long ago, but already it has faded away into the trash bin of so many other failed languages, with over 33,000 commits and less than 400 github stars, nobody gives a fuck, nice try Google+.

The Dart developers made some really bad choices early on when implementing hash-maps, like keeping the order of items [fuckup #1], and not optimizing for translation to Javascript [fuckup #2]. And things get even worse, we can also give up on the Dart VM as part of Chrome in the future, see here.

A major drawback in JavaScript is the lack of typed hash maps, and object keys always being coerced into strings. Objects in JavaScript are unfortunately just associative arrays whose keys are always strings. This can lead to bugs that are hard to trace down. Dart tried to solve this problem with compile time type checking; but this fails in the real world where your code is interfacing with huge amounts of external JavaScript which can not be compile time checked, so you still have to deal with runtime errors.

Typed hashmaps in Rusthon are checked at runtime when you transpile your project without the --release command line option. This allows you to debug your code with runtime errors that make sense, and enforce static types even when working with external JavaScript libraries.

map[K]V{}

Above is the syntax for typed hashmaps in Rusthon, it is directly inspired by the hashmap syntax in Golang. Where K is the key type, and V is the value type. Note this the same syntax used for the Rust, C++ and Go backends. See this example: javascript_typed_dict.md

No comments:

Post a Comment