lundi 3 décembre 2018

Why does if(not nil) give me an ArgumentError?

defmodule My do
  def go do
    x = nil

    if(not x) do
      IO.puts "hello"
    else
      IO.puts "goodbye"
    end
  end
end

In iex:

/elixir_programs$ iex c.exs
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]

Interactive Elixir (1.6.6) - press Ctrl+C to exit (type h() ENTER for help)

iex(1)> My.go
** (ArgumentError) argument error
    c.exs:5: My.go/0

iex(1)> 

According to Programming Elixir >= 1.6, p.35:

Elixir has three special values related to Boolean operations: true, false, and nil. nil is treated as false in Boolean contexts.

It doesn't seem to be true:

defmodule My do
  def go do
    x = false

    if (not x) do
      IO.puts "hello"
    else
      IO.puts "goodbye"
    end
  end
end

In iex:

~/elixir_programs$ iex c.exs
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]

Interactive Elixir (1.6.6) - press Ctrl+C to exit (type h() ENTER for help)

iex(1)> My.go       
hello
:ok

iex(2)> 

Aucun commentaire:

Enregistrer un commentaire