Today I Learned

A Hashrocket project

Ready to join Hashrocket? Find Openings here and apply today.

Guard Clause on Elixir Typespec

TIL that Elixir has some type of guard clause for typespec:

defmodule TupleMaker do
  @spec build(arg1, arg2) :: {arg1, arg2} when arg1: atom, arg2: integer | binary
  def build(arg1, arg2), do: {arg1, arg2}

I still prefer to use @type instead, but itโ€™s always useful to know alternatives.

Looking for help? At Hashrocket, we ๐Ÿ’œ Elixir! From our many Elixir client projects, to sponsoring the Chicago Elixir Meetup, to the source code for this application, we are invested in this community. Contact us today to talk about your Elixir project.