OvermindDL1

OvermindDL1

There is a talk about defguard at Provide defguard · Issue #2469 · elixir-lang/elixir · GitHub 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? ^.^

Most Liked

Eiji

Eiji

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

OvermindDL1

OvermindDL1 OP

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).

Last Post!

OvermindDL1

OvermindDL1 OP

Where Next? Top

Trending in Announcing Top

woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
woylie
I released Doggo, a collection of unstyled Phoenix components. https://github.com/woylie/doggo Features Unstyled Phoenix components....
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
anuaralfetahe
Hello Published a new library - ProcessHub! ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
sergio
It’s not that it’s vocabulary is too advanced. It’s something worse. I get lost trying to follow even a paragraph written by Claude. It’...
New
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews