6 ms·
[I am the author of the post.] I know that keyword lists are different from maps, but I think their superficial similarity to maps isn’t ideal, and I don’t thin
by SupremumLimit 3y ago
[I am the author of the post.] I know that keyword lists are different from maps, but I think their superficial similarity to maps isn’t ideal, and I don’t think that they’re the best possible solution (eg consider pattern matching on keyword lists which is order dependent, it’s not intuitive in the case of named args). They’re confusing to newcomers because of these things.
- sethcalebweeks 3y agoKeyword lists were certainly confusing as a newcomer. But, FWIW, they existed well before maps. The AST is made up of only tuples and keyword lists on top of atoms and other literals. (Actually, if I'm not mistaken, keyword lists themselves are just lists with tuples of atom/value pairs. If you dig far enough down, it's incredible just how much of the language is built on top of a tiny number of primitives. Almost every bit of syntax you can think of is probably several layers of macros.)
- bobwaycott 3y ago> Actually, if I'm not mistaken, keyword lists themselves are just lists with tuples of atom/value pairs. You are correct. A keyword list is `[{atom, any}]`.
- rkangel 3y agoKeyword lists are from Erlang's history really. Erlang didn't have maps or records for a long time and used keyword lists to perform a similar function.
- sph 3y ago> They’re confusing to newcomers because of these things. Then they learn how they work and stop being confused. I struggle to see why we are so terribly afraid of people learning. Programming is not an innate instinct after all. A programmer that doesn't understand linked lists yet is basically a newbie, but it's a topic you learn in one day and makes you a better programmer. This should be encouraged, rather than listed as a con.
- foldr 3y agoThe main problem with them in Elixir is that you can't pattern match on keyword arguments without imposing an arbitrary order.
- sph 3y agoOf course, they're linked list, they have an inherent order. Once you understand how they behave, you understand where the abstraction leaks.
- foldr 3y agoYes, I understand why this is the case. However, it is still a mild annoyance that you cant (sensibly) write e.g. def foo(x, opt1: opt1, opt2: opt2) If optional arguments were first-class citizens in the language, then you'd presumably be able to use patterns along these lines.
- sph 3y agoI get that. But it's built on top of the Erlang VM, neither Erlang nor it's VM support these. All languages have their own leaky abstractions, and stuff that should in theory work, but doesn't.
- josevalim 3y agoIt is unfortunately much trickier because keyword lists are also optional. So what happens if opt2 is not given? We could raise but... since they are optional, we wouldn't want it to raise but rather have a default value. Of course, we could then say "let's allow a default value to be given when pattern matching keyword lists". The problem is that no other pattern works like this and it would be inconsistent. I think optional arguments are fundamentally incompatible with pattern matching (in Elixir). I understand why we would want that but, if we did have it, we would be adding tons of complexity and it would stand out as a sore thumb. I am also wary of going towards the same direction as Ruby and Python which have quite complex argument handling (albeit we have slightly different feature sets).
- sodapopcan 3y agoAh yes, sorry, you did say you understood them in the article but wasn't obvious you understood cases where one was better suited than the other. Otherwise very nice article and sorry I only focused on the rebuttal points! I absolutely agree about grouped alias/import/require. I know those weren't always in the language and I always assumed it was a community request (but maybe not). I personally prefer to avoid `alias` and `import` whenever possible! There a handful of scenarios where I use them but I love how Elixir is big on locality. Any jumps I can avoid, even to the top of a file, is a win in my book. Otherwise the focus on pragmatism is apt. Possibly my favourite thing about Erlang is that it is the only currently widely adopted general purpose language that was built to solve an actual business problem, and that problem so closely resembles web programming. All of the language decisions were made in support of their business goal as opposed to trying to come up with a beautiful, academically sound, language. Something about that really resonates with me so I kinda enjoy and embrace the "warts". Call it Stockholm Syndrome if you must :)