I am currently trying to implement a guard which validates that a parameter is a valid URL. Is it possible to call URI.parse or Regex.match inside a guard clause? I am a newbie that is learning Elixir, and maybe I totally misunderstand guards.
Unfortunately, it’s not… You can see this at the top of this document.
From the doc.
Not all expressions are allowed in guard clauses, but only a handful of them. This is a deliberate choice. This way, Elixir (and Erlang) can make sure that nothing bad happens while executing guards and no mutations happen anywhere. It also allows the compiler to optimize the code related to guards efficiently.
One of the main reasons for this is if you could have an arbitrary function in a guard clause it could have some side effect when run, which would happen even if that function clause should not be run. In haskell you can prevent this but not in Erlang/Elixir