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 ![]()
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
hauleth
Require modules and do qualified calls, it should still work
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
importthem (you could alsorequirethem if you wanted). Compile-time pretty much boils down to “inside the module’s body” though also applies to guards: the block given towhenis evaluated at compile time only, as opposed to the block given todowhich is evaluated at runtime. This is why the scoping trick won’t work as theimportinside the function’s body is checked at runtime.Finally, the reason you can’t rename functions on
importis becauseimportdoesn’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 doimport Foo, only: [bar: 1]then callbar(), the compiler essentially rewrites it toFoo.bar(). I suppose function aliases could be a thing, though I never really thought of itIn any event, all that to say, an option would be to just
requireinstead of import, then you can do:EDIT: Oh, @hauleth beat me to the same advice
I just shouldn’t have been so wordy 
a-maze-d
Thanks for the detailed explanation. It sounds that
requirewould do the trick. I’ll give that a try and report back.thx!
a-maze-d
This works. Thanks for your help