ijverig

ijverig

Error defining a guard - invalid syntax in defguard

Using guards like this works fine:

iex(1)> defmodule M do
...(1)>   def update_position({pos, max_pos}) when pos < max_pos, do: pos + 1                     
...(1)> end
{:module, M, …
iex(2)> M.update_position({3,4})
4
iex(3)> M.update_position({3,3})
** (FunctionClauseError) no function clause matching in M.update_position/1    
    
    The following arguments were given to M.update_position/1:
    
        # 1
        {3, 3}
    
    iex:2: M.update_position/1

But when extracting it into a defguard, the compiler gives a syntax error:

iex(1)> defmodule M2 do                                                                       
...(1)>   defguard is_valid_position({pos, max_pos}) when pos < max_pos                         
...(1)> end
** (ArgumentError) invalid syntax in defguard is_valid_position({pos, max_pos})
    (elixir) lib/kernel.ex:4623: anonymous fn/2 in Kernel.validate_variable_only_args!/2
    (elixir) lib/enum.ex:737: Enum."-each/2-lists^foreach/1-0-"/2
    (elixir) lib/enum.ex:737: Enum.each/2
    (elixir) lib/kernel.ex:4592: Kernel.define_guard/3
    (elixir) expanding macro: Kernel.defguard/1
    iex:2: M2 (module)

Anyone has a clue of the problem here?

Most Liked

Eiji

Eiji

@ijverig: You can solve this by:

defmodule M do
  defguard is_valid_position(tuple) when tuple_size(tuple) == 2 and elem(tuple, 0) < elem(tuple, 1)
end
NobbZ

NobbZ

Yeah that would work, but I prefer the 2 arity version as it makes no assumption about how pos and max_pos are related together.

In any case I’d add is_integer/1, is_float/1 or is_number/1 to the guard, it will safe a lot of time spent on debugging when the first time something not numeric slips into the tuple…

NobbZ

NobbZ

You can not pattern match in a defguard.

Last Post!

OvermindDL1

OvermindDL1

Yeah this is a fantastic set, it essentially absorbed my defguard experiment (that would have worked as OP wanted if it was part of Elixir) but in a way that is stable. :slight_smile:

Where Next?

Popular in Questions Top

Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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