Qqwy

Qqwy

TypeCheck Core Team

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:

First Post! Switch mode

net

net

https://github.com/vic/expat#guarding-patterns

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 91898 914
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
byu
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project. My initial shotgu...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
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

Other Trending Topics Top

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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement