a-maze-d

a-maze-d

Hello all,

I got an issue in defining a defguard which is a combination of already existing defguards. Example:

defmodule Combined do
  import A
  import B

  defguard is_color_name(atom) when 
    is_color_name_from_A(atom) or
    is_color_name_from_B(atom)
end

defmodule A do
  defguard is_color_name_from_A when ...
end

defmodule B do
  defguard is_color_name_from_B when ...
end

The above should (I haven’t tried it) work, but in my case the guard names in the modules A and B are the same (is_color_name coming from different sources)

It seems that the guards NEED to be imported otherwise I get a cannot call remote function inside guard when trying to alias them. I also can’t use the lexical scope trick mentioned in the documentation by wrapping it in a function (because I would still need to import A and B) and I also can’t rename the functions/guards during import (like renaming aliases

I can solve my problem in a not so elegant way (because I have control over A and B), but I would prefer to learn about some trick that could help to solve this problem (and if there is none, then maybe the question whether something is missing in the language)

Happy to hear your (expert) ideas :slight_smile:

Showing Posts 1 to 4

hauleth

hauleth

Require modules and do qualified calls, it should still work

sodapopcan

sodapopcan

In short, no there is no way around this (AFAIK, at least).

Guards are macros that are run at compile time, this is why you need to import them (you could also require them if you wanted). Compile-time pretty much boils down to “inside the module’s body” though also applies to guards: the block given to when is evaluated at compile time only, as opposed to the block given to do which is evaluated at runtime. This is why the scoping trick won’t work as the import inside the function’s body is checked at runtime.

Finally, the reason you can’t rename functions on import is because import doesn’t work like I’m assuming you think it does. It doesn’t “bring functions into a module” it really just “opens up its scope for function resolution,” hence the name “remote” function. When you do import Foo, only: [bar: 1] then call bar(), the compiler essentially rewrites it to Foo.bar(). I suppose function aliases could be a thing, though I never really thought of it :thinking:

In any event, all that to say, an option would be to just require instead of import, then you can do:

defmodule Combined do
  require A
  require B

  defguard is_color_name(atom) when 
    A.is_color_name_from_A(atom) or
    B.is_color_name_from_B(atom)
end

EDIT: Oh, @hauleth beat me to the same advice :upside_down_face: I just shouldn’t have been so wordy :sweat_smile:

a-maze-d

a-maze-d OP

Thanks for the detailed explanation. It sounds that require would do the trick. I’ll give that a try and report back.

thx!

a-maze-d

a-maze-d OP

This works. Thanks for your help

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews