HomePage | RecentChanges | Preferences

Elixir Record

wless1 [4:43 PM] random question: can anyone explain to me what this code is actually doing in elixir: ```{:Elixir.IO}.inspect```

wless1 [4:43 PM] outputs: ```{IO}```

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] exactly

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


HomePage | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited September 25, 2015 5:32 am by (diff)