Qqwy

Qqwy

TypeCheck Core Team

Idea: 'multiguards' to allow structs in guards

Elixir allows you to define your own data-structures as structs, on top of Erlang maps. However, such user-defined types have always felt a little ‘off’ from the built-in types, because they do not get the same level of support. Most notably, it is (used to be) virtually impossible to define a guard clause that properly works with your struct, because of two problems:

  1. Destructuring maps inside a guard clause was not possible. OTP 21 changes this, with the introduction of the guard-safe :erlang.map_get/2 and :erlang.is_map_key/2 functions.
  2. It is not possible to perform conditionals inside a guard clause; the thing that comes the closest is :erlang.andalso/2/:erlang.oreither/2 (in Elixir exposed as and and or respectively), which however require that the left-hand-side always evaluates to a boolean.

However, today I realized that there is a solution for (2): We could create a compile-time macro that creates multiple function-heads with the different versions of the macro below one-another!

An example

I have in the past created the library Ratio that allows rational numbers. It would be great if you could just use the built-in arithmetic and comparison operators on these structures. However, until now it was impossible to do so: We want to be able to

  • compare %Ratio{} to %Ratio{}
  • %Ratio{} to integer,
  • integer to %Ratio{},
  • and still allow integer ↔ integer comparisons.

If def and defp were to be altered to, whenever a comparison operation would be used in a guard, expand the function into multiple clauses, each with the next guard, then these kinds of operations would now be possible!

Important advantages

  • It will finally be possible to treat our custom types as built-in ones, by having proper guard-safe functions work on them.
  • Related to this, it will be possible to override or extend the built-in operators so they will work properly for custom datastructures as well, which will (a) remove one hurdle for newcomers when coming to the language and (b) improve the readability of our code.

Disadvantages

Two disadvantages come to mind:

  1. The difference between the code you write and what it compiles to increases slightly. This might have an impact on the debug-ability of some code. However, because you see, for instance, the expanded guards right next to an ArgumentError-stacktrace and because people should not nest multiple layers of abstraction in the same guard clause anyway, I think this might not be that much of a disadvantage.
  2. Increased code size. Either the function body is copied multiple times, ones per guard clause, or an extra level of call-indirection is done. Either might be optimized away by the BEAM however (I have not yet done any checks to see this). Another possibility would be to compile such a multi-guard down to an Erlang ,-separated (comma-separated) guard clause set, which is the built-in way Erlang allows multiple clauses to dispatch to the same function body.

All right, that was all I had to say for now.

Opinions? Ideas? Suggestions? :slight_smile:

Most Liked

net

net

Where Next?

Popular in Discussions Top

matthias_toepp
I’d love to hear what people think about Wisp, the new Gleam web framework started by Gleam’s primary creator Louis Pilfold. Gleam, alon...
New
sashaafm
I’m trying to evaluate the best combo/stack for a BEAM Web app. Right now I’m exploring Yaws a bit, after having dealt with Phoenix for a...
New
arpan
Hello everyone :wave: Today I am very excited to announce a project that I have been working on for almost 3 months now. The project is...
New
owaisqayum
I have a sample string sentence = "Hello, world ... 123 *** ^%&*())^% %%:>" From this string, I want to only keep the integers, ...
New
axelson
Decided against including more info in the title, but the gist is that Plataformatec sponsored projects will continue with the assets bei...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
rms.mrcs
A couple of days ago I was discussing with a friend about different approaches to write microservices. He said that if he was going to w...
New
mbenatti
Following https://github.com/tbrand/which_is_the_fastest |> [image] Updated with Elixir^ Now t...
New
PragTob
Hey everyone, this has been brewing in my head some time and it came up again while reading Adopting Elixir. GenServers, supervisors et...
New
Markusxmr
Since Drab has been developed for a while in the open, introducing the Liveview functionality in a way it happend appears to undermine th...
New

Other popular topics Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30840 112
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126226 1237
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement