History of ElixirRecord

HomePage | RecentChanges | Preferences

Revision 3 . . December 22, 2019 10:59 am by 78-93-98-198.dsl.wavetel.us
Revision 2 . . September 25, 2015 5:32 am by

Difference (from prior major revision) (no other diffs)

Changed: 1,51c1

Elixir Record

wless1 [4:43 PM]
random question: can anyone explain to me what this code is actually doing in elixir:

wless1 [4:43 PM]

mrluc [4:52 PM]
It does? Not working in my IEx.

wless1 [4:54 PM]
```Erlang/OTP 18 [erts-7.0.2] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Interactive Elixir (1.0.5) - press Ctrl+C to exit (type h() ENTER for help)```

gregvaughn [4:57 PM]
It's the record syntax. It came up last week too. Records, under the covers, are tagged tuples (the first element is an atom of a module name). `{:SomeAtom?, x, y}.blah(z)` is equivalent to `SomeAtom?.blah(z, {:SomeAtom?, x, y})`

gregvaughn [4:57 PM]
So you're saying `Elixir.IO.inspect( {:Elixir.IO})`

gidrek [4:58 PM]
In Elixir, modules are atoms, IO is an alias for :Elixir.IO, inspect prints the representation of an Elixir term (no always)

keathley [4:59 PM]
joined #general

gregvaughn [5:01 PM]
Records are precursors to Structs, but they're still useful for interop with erlang. Note: if you use this to try to make things OO-ish, you'll make Josť cry.
:joy:1 :+1:3

wless1 [5:02 PM]
I see. I thought I was accessing a field of a struct, but it was in fact a tuple - and the error message was confusing

wless1 [5:03 PM]
since elixir was interpreting it as calling a method on a record module

gregvaughn [5:03 PM]
Your error message was probably that a module by the name of your first element of the tuple is not found

wless1 [5:03 PM]

wless1 [5:05 PM]
thanks for the explanation!


HomePage | RecentChanges | Preferences