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? ^.^

Showing Posts 1 to 3

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

OvermindDL1

OvermindDL1 OP

— All posts loaded —

Where Next? Top

Trending in Announcing Top

bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
387 15136 120
New
wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New
handnot2
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application. This library uses Erlang esaml to provide plug enabl...
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 11030 135
New
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New

Other Trending Topics Top

mudasobwa
I am seeing a lot of aplications of Argumentum ad Vericundiam in software discussions. They do link some piece of writing and point us to...
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
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
sorenone
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
lawik
I was thinking since Goatmire Elixir turned out pretty good I should maybe do another one. 30th of Sep - 2nd of Oct this year./ The firs...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews