【发布时间】:2018-12-03 10:55:10
【问题描述】:
defmodule My do
def go do
x = nil
if(not x) do
IO.puts "hello"
else
IO.puts "goodbye"
end
end
end
在 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)>
根据Programming Elixir >= 1.6,第35页:
Elixir 具有三个与布尔运算相关的特殊值:true、 假的,零。 nil 在布尔上下文中被视为 false。
这似乎不是真的:
defmodule My do
def go do
x = false
if (not x) do
IO.puts "hello"
else
IO.puts "goodbye"
end
end
end
在 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)>
【问题讨论】:
-
您是否尝试过切换到 cond 并查看它是否可以编译?或者将语句切换到 if x do IO.puts "goodbye" else IO.puts "hello"
标签: if-statement null boolean elixir