Expat - composable, reusable pattern matching

Hi @vic,

I really enjoy the continuous work you have been doing in expat!

I wonder if we should support when clauses inside patterns and automatically move them out. So if you do:

def foo(x when is_integer(x)), do: x

It becomes:

def foo(x) when is_integer(x), do: x

while nobody would write the first in practice, it is very powerful for extending the language. It means you would be able to drop the expat prefix in many cases.

Similarly, regarding the union types, I wonder if instead of the expat prefix, you could use the prefix of the union, so this example becomes:

  test "nil cannot be pattern matched with just" do
    maybe case Keyword.get([], :foo) do
      just() -> raise "Should not happen"
      nothing() -> assert :ok
    end
  end

Or even:

  test "nil cannot be pattern matched with just" do
    maybe Keyword.get([], :foo) do
      just() -> raise "Should not happen"
      nothing() -> assert :ok
    end
  end

Although I am not sure how that would play with nesting. For example, if you have a maybe with a nat inside, how would you handle it? Do you need two separate cases? I guess that would be the case, as a nat with a nothing doesn’t make sense. I wonder if other languages (OCaml/Haskell/F#) allow case to be “folded” in such cases or do they require an explicit case per pattern? /cc @OvermindDL1

Thoughts?

3 Likes