Bad Ideas : Elixir

A Module Named 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?

defmodule :true do
  def x, do: true
try it out:
iex> true.x
iex> true.x.x
iex> true.x.x.x
iex> (not false).x
iex> (7 > 3) .x
iex> true and :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.