I am learning Elixir (have some exp in Ruby, JS). Have an interesting question. Just out of curiosity and for better understanding of the functional programming.
From the documentation:
if(condition, clauses)
Provides an if/2 macro. This macro expects the first argument to be a condition and the second argument to be a keyword list.
In other words I can write:
if(true, [do: "true stuff", else: "false stuff"]) # => "true stuff"
And this line is working perfectly. Ok.
But as far as I aware keywords lists are lists of tuples. So I've tried:
if(true, [{:do, "true stuff"}, {:else, "false stuff"}]) # => "true stuff"
Still working!
Good. Lets do next step:
var = "true stuff"
if( true, [{:do, var}, {:else, "false stuff"}]) # => "true stuff"
Nice. Now lets be crazy!
var = {:do, "true stuff"}
if( true, [var, {:else, "false stuff"}])
** (ArgumentError) invalid or duplicate keys for if, only "do" and an optional "else" are permitted
(elixir) lib/kernel.ex:2569: Kernel.build_if/2
(elixir) expanding macro: Kernel.if/2
iex:18: (file)
Oops!
And if we just try it in 'iex' everything is Ok:
var = {:do, "true stuff"}
[var] # => [do: "true stuff"]
Why Elixir's 'if' macros don't "recognize" this "good to eat tuple" ?
Was such a thing made by purpose ?