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:

Showing Posts 1 to 1

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 94592 917
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
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
heathen
Quite interesting article Google brought me. Didn’t find any mentions about it here. What do you think in general? Would you use togethe...
New
maennchen
:warning: Security advisory: Decimal DoS vulnerability A vulnerability has been published for decimal where very large exponents can cau...
New
marciol
It would be helpful to have a list of companies worldwide that hire engineers without prior experience in Elixir. Often, it can be quite ...
New
durvia
Anyone running long-lived stateful processes on BEAM? We’re building an AI agent runtime and would love to compare notes. We’re a small ...
New

Other Trending Topics Top

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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
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
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews