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

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

Where Next?

Popular in Discussions Top

Jayshua
I recently came across the javascript library htmx. It reminded me a lot of liveview so I thought the community here might be interested....
New
laiboonh
Hi all, I am trying to convince my team to use liveview over the current react. What are some of the points where one should consider us...
New
mmport80
I have put far too much effort into Dialyzer over the last year or so - and basically - I doubt it’s worth the effort. It’s not as easy ...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31307 143
New
nunobernardes99
Hi there Elixir friends :vulcan_salute: In a recent task I was on, I needed to check in two dates which of them is the maximum and which...
New
AlexMcConnell
The reason that Rails is as popular as it is is because it’s very easy for relatively inexperienced developers to get a lot of work done....
588 19675 166
New
fireproofsocks
I’ve been working on an Elixir project that has required a lot of scripting. I usually reach for Elixir because I like it more (and in th...
New
jer
I’ve been using umbrellas for a while, and generally started off (on greenfield projects at least) by isolating subapps based on clearly ...
New
AstonJ
Can you believe the first professionally published Elixir book was published just 8 years ago? Since then I think we’ve seen more books f...
New
griffinbyatt
Sobelow Sobelow is a security-focused static analysis tool for the Phoenix framework. For security researchers, it is a useful tool for g...
New

Other popular topics Top

WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39523 209
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
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36432 110
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New

We're in Beta

About us Mission Statement