Accessing Structs in Elixir

Elixir is a language that runs on the Erlang VM (BEAM) and looks a bit like ruby. It caught my interest, because I wanted to learn something that was different and not very mainstream, a little inspired by the 12 resolutions for programmers post.

Update: check out the suggestions and examples in the reddit thread too

I found myself very confused and googled all kinds of things like

  • How to access property of elixir object
  • How to access key of elixir object
  • How to print property of elixir struct

until I found an example that showed that variable assignment works very much like in Go or the error first callbacks in node.js.

I was trying to print a certain value that belogs to a key on a struct for File.stat like in the following example:

myFile = File.stat("hello.txt")
IO.inspect myFile.access

// output:
** (UndefinedFunctionError) undefined function :ok.access/1 (module :ok is not available)
    :ok.access({:ok, %File.Stat{access: :read_write, atime: {{2016, 1, 27}, {13, 7, 22}}, ctime: {{2016, 1, 27}, {13, 7, 22}}, gid: 1000, inode: 11545547, links: 1, major_device: 41, minor_device: 0, mode: 33188, mtime: {{2016, 1, 27}, {13, 7, 22}}, size: 20, type: :regular, uid: 1000}})
    (file)
    (elixir) lib/code.ex:363: Code.require_file/2

The trick is to just use two variables from the very beginning, status of the function first.

{status, myFile} = File.stat("hello.txt")
IO.inspect myFile.access
//output
test1.ex:1: warning: variable status is unused
:read_write

Again, this might be a problem NOBODY else has, but I didn’t see a lot of places it was described in the getting started or beginner guides. Only through the File.stat documentation I got the idea to search for structs and look through some examples.

I thought my mistake was that I was trying to access the values of the struct wrongly, but it was actually the variable assignment in elixir that I buggered up. Actually the idea of the error first returns/callbacks is great, because you can test if a previous function has failed before trying to make the rest of your functions cope with some undefined value.

A really neat thing is that the compiler will tell you that you’re not using a defined variable.

Thank you for reading! If you have any comments, additions or questions, please leave them in the form below! You can also tweet them at me

If you want to read more like this, follow me on feedly or other rss readers

4 thoughts on “Accessing Structs in Elixir”

  1. It seems like errors are not handled in the code sample;
    Personally I’d change this `{status, myFile} = File.stat(“hello.txt”)` to `{:ok, myFile} = File.stat(“hello.txt”)` (pattern matching on the :ok status; in case of an error it automatically blows up with the error message)

    1. Thanks for your comment! True, I was just trying to help out other beginners that might be confused by the multiple returns. That’s a good idea, then you don’t have to write any if/else things in it!

Leave a Reply

Your email address will not be published. Required fields are marked *