Elixir 1.17's ✨ new `to_timeout/1` helper!

Notes

In my previous tip, I shared how we can use :timer helpers to remove magic millisecond numbers from our codebase. That makes it easier for our co-workers (and ourselves!) to understand our code’s intent.

Well, in Elixir 1.17, we don’t even need to use those :timer helpers! 😲

It ships a brand ✨ new to_timeout/1 helper.

The cool thing about it is that it takes:

  • Brand new duration structs,
  • hour, minute, etc. components, or
  • regular timeout arguments like integers and :infinity

Just like this:

iex> to_timeout(%Duration{hour: 1, minute: 30})
# => 5400000

iex> to_timeout(hour: 1, minute: 30)
# => 5400000

iex> to_timeout(300_000)
# => 300000

iex> to_timeout(:infinity)
# => :infinity

Want the latest Elixir Streams in your inbox?

    No spam. Unsubscribe any time.