-
- Downloads
There was an error fetching the commit references. Please try again later.
Remove hash map literals
In order to simplify the syntax, hash map literals have been removed. Instead, one now creates a hash map using `Map.new`. To make it easier to create a map with a bunch of pairs in one expression, we introduce the `Map.set` method. This method behaves similar to `Map.[]=`, but returns the Map itself instead of the value written: Map .new .set('foo', 'bar') .set('baz', 'quix') So why were map literals removed? Well, first of all their syntax was not intuitive: `%[key: value]`. This syntax was taken from Elixir, but is not used in other languages that we are aware of. Many languages use curly braces (e.g. `{key => value}`), but these are already used for closures. Some languages reuse square brackets (e.g. `[key: value]`), but this makes the meaning of `[]` unclear. We considered using a syntax similar to Scala: Map.new(key -> value) Here `->` would be a method that returns some sort of tuple, and `Map.new` would take this list of tuples and use them to fill the map. The method `->` would have to be available for every object, since it's perfectly valid to use outside of constructing maps. This means `->` would have to be defined on `Object`, or in a trait that is implemented for it. Manually implementing the method/trait would be too cumbersome. The hypothetical code for this might look as follows: impl Object { def ->!(V)(other: V) -> Tuple!(Self, V) { Tuple.new(self, other) } } Unfortunately, the Ruby compiler does not support the use of self types in generic types well enough to make this work. This is a long standing issue [1], but it would require an extensive rewrite of the type system to support. Since we want to rewrite the Ruby compiler in Inko, adding support for this in the Ruby compiler would be a waste of time. There are a variety of other approaches, such as passing a closure to `Map.new` that can be used to fill up the map. All of these suffer from similar problems: the Ruby compiler's type system is a bit buggy. To work around all of this, we added the `Map.set` method. While the resulting code is a bit more verbose, it does not require any compiler changes. The API should also feel familiar to those used to immutable programming languages, which typically use a similar approach for constructing hash maps. The removal of map literals also allows us to remove various compiler optimisations of these literals, simplifying the compiler and making the language more predictable. [1]: https://gitlab.com/inko-lang/inko/issues/107
Showing
- compiler/lib/inkoc/ast/predicates.rb 0 additions, 4 deletionscompiler/lib/inkoc/ast/predicates.rb
- compiler/lib/inkoc/ast/send.rb 0 additions, 8 deletionscompiler/lib/inkoc/ast/send.rb
- compiler/lib/inkoc/config.rb 0 additions, 3 deletionscompiler/lib/inkoc/config.rb
- compiler/lib/inkoc/lexer.rb 3 additions, 15 deletionscompiler/lib/inkoc/lexer.rb
- compiler/lib/inkoc/parser.rb 0 additions, 38 deletionscompiler/lib/inkoc/parser.rb
- compiler/lib/inkoc/pass/define_type.rb 0 additions, 2 deletionscompiler/lib/inkoc/pass/define_type.rb
- compiler/lib/inkoc/pass/generate_tir.rb 0 additions, 64 deletionscompiler/lib/inkoc/pass/generate_tir.rb
- compiler/spec/inkoc/lexer_spec.rb 3 additions, 11 deletionscompiler/spec/inkoc/lexer_spec.rb
- runtime/src/core/bootstrap.inko 1 addition, 0 deletionsruntime/src/core/bootstrap.inko
- runtime/src/std/compiler/lexer.inko 11 additions, 22 deletionsruntime/src/std/compiler/lexer.inko
- runtime/src/std/env.inko 2 additions, 2 deletionsruntime/src/std/env.inko
- runtime/src/std/ffi.inko 3 additions, 3 deletionsruntime/src/std/ffi.inko
- runtime/src/std/inspect.inko 13 additions, 4 deletionsruntime/src/std/inspect.inko
- runtime/src/std/map.inko 35 additions, 32 deletionsruntime/src/std/map.inko
- runtime/tests/test/std/compiler/test_lexer.inko 0 additions, 10 deletionsruntime/tests/test/std/compiler/test_lexer.inko
- runtime/tests/test/std/test_ffi.inko 11 additions, 7 deletionsruntime/tests/test/std/test_ffi.inko
- runtime/tests/test/std/test_inspect.inko 12 additions, 2 deletionsruntime/tests/test/std/test_inspect.inko
- runtime/tests/test/std/test_map.inko 91 additions, 38 deletionsruntime/tests/test/std/test_map.inko
Please register or sign in to comment