Quick extraction of elem from tuple - best practice?

Hi,

I find myself doing something like this, mostly in tests:

{:ok, new_time} = Time.new(15, 00, 00)
      valid_attrs = %{
        body: "some body",
        time: new_time
      }

I wonder if it’s ok to extract the tuple value like this

valid_attrs = %{
        body: "some body",
        time: Time.new(15, 00, 00) |> elem(1)
      }

I know I can do that, the question is whether I should be doing that this way? Thank you

The downside in this specific case is that you are ignoring possible errors. If Time.new returns {:error, :some_error_condition} then some_error_condition will end up in your valid_attrs map. This is in general not good :). But, if you are 100% sure that Time.new will always return a valid time, then it’s ok.

1 Like

Yeah I thought about that one. But since it’s only in tests, I guess it’s ok.

I think you’re looking for Time.new! or ~T:

Time.new!(15, 00, 00)
~T[15:00:00]
4 Likes

omg of course :see_no_evil:

1 Like