Using 'not in' in a guard phrase

I think I should be able to use ‘not in’ in a guard, but it doesn’t seem to work:

$ elixir -v
Erlang/OTP 19 [erts-8.2] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]

Elixir 1.3.4
$ cat t.exs
defmodule X do
  def y(n, m..p) when n not in m..p, do: IO.puts "OK"
end
$ iex t.exs
Erlang/OTP 19 [erts-8.2] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]

** (SyntaxError) t.exs:2: syntax error before: in
    (elixir) lib/code.ex:363: Code.require_file/2

$ 

The exact same thing works without the ‘not’; what am I missing?

1 Like

First note, Discourse is standard markdown, like what is used on github, you can delinate code with:
```elixir
code here
```
And with your code in your post it appears like:

$ elixir -v
Erlang/OTP 19 [erts-8.2] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]

Elixir 1.3.4
$ cat t.exs
defmodule X do
def y(n, m..p) when n not in m..p, do: IO.puts "OK"
end
$ iex t.exs
Erlang/OTP 19 [erts-8.2] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]

** (SyntaxError) t.exs:2: syntax error before: in
(elixir) lib/code.ex:363: Code.require_file/2

The issue is not the not but rather where it is placed. not in Elixir is a pre-fix call, basically it is like a function of not(something) except you can do it as not something. So by putting n not in m..p it is like saying n(not(in m..p)) which makes no sense and does not compile. You are probably wanting not (n in m..p). :slight_smile:

For note, I ‘think’ Elixir is adding a not in infix operator in a version so what you have should work then, but I do not think it is in 1.3, ‘maybe’ 1.4? Speaking of, your Elixir is quite out of date, you should update it to at least 1.4 or newer. :slight_smile:

[quote=“OvermindDL1, post:2, topic:5672”]
For note, I ‘think’ Elixir is adding a not in infix operator in a version so what you have should work then[/quote]
It’s in the documentation for v1.5.0-dev Kernel.in/2 (and doesn’t work in 1.4.4).

1 Like

Thanks! Now have 1.4.4, and understand the issue.

Cool thanks! I did not know what version it was introduced. I run git master often. :slight_smile:

No problem, and apparently it is introduced in 1.5, so it shall be out soon or you can build git master. :slight_smile: