Is there a Elixir equivalent of Rails `try` function?

Viewed 1260

In Rails you can do something like:

@user.try(:contact_info).try(:phone_number)

Which will return the phone_number if the @user and contact_info are both not nil. If one of those are nil, the expression will return nil.

I want to know what is the most idiomatic way to do this in Elixir, knowing that @user and contact_info are structs.

7 Answers
Related