6 ms·
Elixir Gotchas
- arrowsmith 2y agoThis is slightly out of date: > The other mystery here are the 2 different syntaxes in use for charlists – single quotes ('charlist') vs. the ~c"charlist" sigil. That’s a rather recent development, it was changed in Elixir 1.15, after some discussion … > > So, it’s now less confusing but still confusing – which is why it made this list. Charlists with single quotes are deprecated since Elixir 1.17 - the ~c[…] sigil should always be preferred. So hopefully that will reduce the confusion!
- seventyone 2y agoyou can also use access methods on structs in 1.17 so you don't need the Access.key hack with get_in
- bongobingo1 2y agoI'm not sure this is true? Or I misinterpret. Erlang/OTP 27 [erts-15.0] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit:ns] Interactive Elixir (1.17.1) - press Ctrl+C to exit (type h() ENTER for help) iex(1)> defmodule X, do: defstruct [:a] {:module, X, <<70, 79, 82, 49, 0, 0, 7, 128, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 244, 0, 0, 0, 22, 8, 69, 108, 105, 120, 105, 114, 46, 88, 8, 95, 95, 105, 110, 102, 111, 95, 95, 10, 97, 116, 116, 114, ...>>, %X{a: nil}} iex(2)> x = %X{a: 1} %X{a: 1} iex(3)> x[:a] ** (UndefinedFunctionError) function X.fetch/2 is undefined (X does not implement the Access behaviour You can use the "struct.field" syntax to access struct fields. You can also use Access.key!/1 to access struct fields dynamically inside get_in/put_in/update_in) X.fetch(%X{a: 1}, :a) (elixir 1.17.1) lib/access.ex:322: Access.get/3 iex:7: (file) iex(4)> get_in(x, [:a]) ** (UndefinedFunctionError) function X.fetch/2 is undefined (X does not implement the Access behaviour You can use the "struct.field" syntax to access struct fields. You can also use Access.key!/1 to access struct fields dynamically inside get_in/put_in/update_in) X.fetch(%X{a: 1}, :a) (elixir 1.17.1) lib/access.ex:322: Access.get/3 iex:7: (file) iex(5)> get_in(x, [Access.key!(:a)]) 1
- neillyons 2y agoget_in(x.a) better example would be nested. get_in(x.a.b.c)
- sph 2y agoThis would be more helpful if it dove into the Erlang side, as many of these "gotchas" are due to how Erlang and the BEAM work, and Elixir, being built on top of it and binary-compatible with the entire ecosystem, necessarily exhibits these behaviours. Keyword lists, charlists vs strings (actually called binaries) [1], empty map matching all maps are obvious if you look at how they work in Erlang. I haven't written a single line of Erlang though I can grok it pretty well by now. Knowing the entire platform is how you become a productive Elixir programmer, though it seems some devs refuse to look behind the curtains to understand the Erlang side of it, thus never truly understand the power of the BEAM platform. --- 1: I admit I still unsure about the separation of charlists and binaries. As far as I understand it, binaries are stored in their own heap to avoid copying when sending across processes, so are ideal for sharing large payloads, and charlists are not great for UTF-8 content, as they simply are a naive list of integers (hence one of the gotchas). Erlang uses charlists a lot for "string" content. Elixir made the right choice to use binaries for strings, as they can ensure they are correctly encoded, provide high-level API to deal with the intricacies of Unicode, and still retain compatibility with Erlang by providing charlists and raw binaries for non-UTF-8 data.
- josefrichter 2y agoEveryone should read https://learnyousomeerlang.com https://learnyousomeerlang.com
- facundo_olano 2y ago+1 I'd say it's a great read even if you don't plan to do much Erlang or Elixir programming.
- josefrichter 2y agoexactly
- sph 2y agoThen, when you have code in production, https://www.erlang-in-anger.com/ https://www.erlang-in-anger.com/ (from the same author)
- fredwu 2y agoAre these even gotchas if they are all very well documented in the official documentation already? I was expecting to see some unusual or undocumented behaviours...
- cutler 2y agoElixir also doesn't have arrays so you have to fall back on Erlang's extremely clunky variant or, worse, shoehorn a map.
- peoplefromibiza 2y agomany functional languages (if not all) do not have arrays (intended as a contiguous areas of memory containing indexable elements of fixed size) they are usually left as an implementation as libraries (i.e. Haskell's Data.Array) There are also easy workarounds to that (including NIFs), I personally never needed arrays in Elixir
- derekkraan 2y agoYeah, just (learn how to) use lists and you'll be fine. Of course if your goal is to re-implement sorting algorithms that were designed for mutable memory, you will be disappointed. This isn't a deal-breaker. If you want a platform language, use that. But for web, you'd be hard-pressed to find something better suited than Elixir.
- gregors 2y agoI've only ran into a single instance where I needed array-like access and since it was a read only type of thing tuples worked perfectly.
- foldr 2y agoNot disagreeing with you, but it's worth noting that tuples support random O(n) access by index. They can therefore be used in place of arrays for some simple applications.
- rdtsc 2y ago> random O(n) access O(1) for reading I believe. The slightly awkward array module is built on top of tuples, a 10-tree of tuples? or something like it. But good point, once in a while I see list_to_tuple to provide faster indexed reads on lists.
- 2y ago
- robxorb 2y agoUnless you are fine letting old homebrew work its "magic" - the installation is too lengthy and involved for a new language. It should just be click, download: https://elixir-lang.org/install.html https://elixir-lang.org/install.html That is the biggest gotcha for me. There doesn't seem to be a simple installer or binary you just get up and running unlike other major languages. Is it a distribution issue with erlang or something? I'd have thought it'd be easy to package it up.
- lionkor 2y agoBlame your package manager, not Elixir? Its one command to install on Arch for example.
- robxorb 2y agoNot requiring a package manager is the point. Python, node, Zig etc supply binaries or a binary installer for each major OS, so why not Elixir? I can't see how it'd be a bad thing for anyone.
- natrys 2y ago> so why not Elixir? I imagine that part is slightly complicated by the fact that it requires host language runtime (Erlang). Elixir compiled with one version of OTP is not necessarily compatible with another version so supporting binaries for N versions of Elixir becomes NxM problem when you factor in OTP versions, plus supporting distribution of OTP binaries is probably complicated in itself. If there is a problem with OTP, users would swarm first party distributor, but their ability to help would be very limited.
- kimi 2y agoI use asdf - https://asdf-vm.com/ https://asdf-vm.com/ - brew sucks if you need fine control of versions, multiple versions, etc.
- deleted 2y ago[deleted]
- deleted 2y ago[deleted]