Defguard - experimenting with a structural matching alternative to Kernel.defguard

There is a talk about defguard at https://github.com/elixir-lang/elixir/issues/2469 and I was curious to see if I could make a defguard powerful enough to implement an is_struct (which requires altering the matching context as well as altering guards). Well I made it, it is a hack, it is simple, would neeed more cleaning up before release if I should release it (unsure if I should) considering the hack that it is, but it works. :slight_smile:

A shell session:

blah@blah MINGW64 $HOME/projects/tmp/defguard
$ iex -S mix
Eshell V8.2  (abort with ^G)
Interactive Elixir (1.4.0) - press Ctrl+C to exit (type h() ENTER for help)
iex>defmodule StructEx do
...>   import Defguard
...>   defguard is_struct(%{__struct__: struct_name}) when is_atom(struct_name)
...> end
{:module, StructEx,
 <<70, 79, 82, 49, 0, 0, 5, 252, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 0, 155,
   131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115,
   95, 118, 49, 108, 0, 0, 0, 4, 104, 2, ...>>, {:is_struct, 1}}
iex> defmodule Testering do
...>   use Defguard
...>   import StructEx
...>   def blah(any_struct) when is_struct(any_struct), do: any_struct
...>   def blah(_), do: nil
...> end
warning: unused import StructEx
{  iex:4
:module
, Testering,
 <<70, 79, 82, 49, 0, 0, 5, 24, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 0, 151,
   131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115,
   95, 118, 49, 108, 0, 0, 0, 4, 104, 2, ...>>, {:blah, 1}}
iex> Testering.blah(%{__struct__: Blah})
%{__struct__: Blah}
iex> Testering.blah(%{blah: 42})
nil
iex> Testering.blah(42)
nil
iex>

As you can see, it works fine. ^.^

I’m also playing with the idea to have defguard accept an optional do/end so it can do compile-time checks and alter the ast directly, but not needed for something as simple as is_struct/1. :slight_smile:

My hack is ‘just’ implemented enough for something this simple to work, would need work to have more, but considering it is less than 60 lines of code to do what I have now and adding more cases is easily expressed with how I have things split up, this could easily become a full defguard hack^H^H^H^Himplementation. ^.^

Overall it has code that makes a new function head internally for each new when condition with the body for each, basically what erlang does internally anyway with multiple whens, just explicit, so it should not have any speed hit over doing it manually (plus multiple whens on a function head is almost never used), just it is much less code this way. But yeah, defining an is_exception would be as simple as:

defguard is_exception(%{__struct__: struct_name, __exception__: true}) when is_atom(struct_name)

And this has the same checking power as Exception.exception?/1 does that is in Elixir now, except it works as a guard, like:

iex> defmodule StructEx do
...>   import Defguard
...>   defguard is_exception(%{__struct__: struct_name, __exception__: true}) when is_atom(struct_name)
...> end
{:module, StructEx,
 <<70, 79, 82, 49, 0, 0, 6, 48, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 0, 158,
   131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115,
   95, 118, 49, 108, 0, 0, 0, 4, 104, 2, ...>>, {:is_exception, 1}}
iex> defmodule Testering do
...>   use Defguard
...>   import StructEx
...>   def blorp(exc) when is_exception(exc), do: "exceptioned"
...>   def blorp(val), do: "No-exception:  #{inspect val}"
...> end
{  iex:5
:module
, Testering,
 <<70, 79, 82, 49, 0, 0, 6, 52, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 0, 148,
   131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115,
   95, 118, 49, 108, 0, 0, 0, 4, 104, 2, ...>>, {:blorp, 1}}
iex> Testering.blorp(42)
"No-exception:  42"
iex> Testering.blorp(%{__struct__: Blah})
"No-exception:  %{__struct__: Blah}"
iex> Testering.blorp(%ArithmeticError{})
"exceptioned"
iex>

So does this seem useful enough to release even if it is a hack? Does anyone else want to work on it since I am very short on time for at least the next few weeks? ^.^

6 Likes

I’m really waiting for feature like that. It will be really usefull in full version. Please continue work on it.

1 Like

Elixir is already working on a Kernel.defguard as linked in the first post, but as it is a macro and not a special syntax it will not have the capabilities that this one does, and would not quite match. Perhaps I should rename mine defpattern instead, hmm…

What Elixir should do is make its defguard actually powerful instead of just being an overglorified macro that we can already easily do. Like the current elixir proposal just implements is_record as:

  defguard is_record(data, kind) when
    is_atom(kind) and is_tuple(data) and tuple_size(data) > 0 and elem(data, 0) == kind

When we can already do it as a macro, as the original version is, so it is not adding any new functionality where my above version actually does (though my implementation is definitely as a hack, it ‘should’ be as syntax).

1 Like

More talk at: https://groups.google.com/forum/#!topic/elixir-lang-core/tu-0yjvB0Kk