true
In elixir modules are usually written as defmodule Foo
. The Foo
is just sugar for the atom :'Elixir.Foo'
. The two are equlivelent. In the
same way true
is just sugar for the atom :true
.
Module names in Erlang and Elixir are atoms. Elixir uses the upper-case convention to namespace Elixir modules. It is a very good idea to follow this convention. But what tricks could we do if we don't follow?
First question: if module names are atoms, and if true
is an atom, does that
mean we can name a module true
?
try it out:
defmodule :true do
def x, do: true
end
iex> true.x
true
iex> true.x.x
true
iex> true.x.x.x
true
iex> (not false).x
true
iex> (7 > 3) .x
true
iex> true and :ok
:ok
Well, not only can we define a module true
, but the module coexists
alongside the exising boolean interpretation. Boolean operators within the language
treat it as a boolean, but the dot operator treats it as a module name.
So that's it, we are free to use true
, false
and nil
to write some obscure code. I must admit I am impressed with how well the language handled my abuse.
Slick.