Jose's Example Code From 2023 Keynote

Ok, I really feel dumb for having to ask this but I’m very curious.

In Jose’s keynote he showed this example of why the new type implementation won’t prevent certain bugs:

def left and right do
  Enum.random([left, right])
end

So just to see if he was just making something up to make his point I tried it in iex and sure enough–there’s an “and” function. What puzzles me is how is it that “left” isn’t the name of the function? Can someone help a confused fellow to understand how it’s possible to have arguments before and after the name of the function?

1 Like

It makes more sense if you look at the AST created by a def like that:

iex(2)> quote do
...(2)>   def left and right do
...(2)>     :ok
...(2)>   end
...(2)> end

{:def, [context: Elixir, import: Kernel],
 [
   {:and, [context: Elixir, import: Kernel],
    [
      {:left, [if_undefined: :apply], Elixir},
      {:right, [if_undefined: :apply], Elixir}
    ]},
   [do: :ok]
 ]}

which looks exactly like a definition of a function:

iex(3)> quote do
...(3)>   def foo(left, right) do
...(3)>     :ok
...(3)>   end
...(3)> end

{:def, [context: Elixir, import: Kernel],
 [
   {:foo, [context: Elixir],
    [
      {:left, [if_undefined: :apply], Elixir},
      {:right, [if_undefined: :apply], Elixir}
    ]},
   [do: :ok]
 ]}

However, the parser doesn’t allow a function to be named a reserved word:

iex(4)> quote do
...(4)>   def and(left, right) do
...(4)>     :ok
...(4)>   end
...(4)> end

** (SyntaxError) iex:5:22: syntax error before: ')'
    |
  5 |   def and(left, right) do
    |                      ^
3 Likes

To add to it, it’s how you define infix operators in Elixir, for example:

def foo <~> bar do
  # ...
end

There are a set number of infix operators, you can’t just make them up, though I can’t remember where they are listed right now (sorry).

A quick way to try out the and version is like this:

defmodule Foo do
  import Kernel, except: [and: 2]

  def left and right do
    Enum.random([left, right])
  end

  def test do
    "foo" and "bar"
  end
end

Foo.test()
2 Likes

https://hexdocs.pm/elixir/1.12.3/operators.html#defining-custom-operators

4 Likes

Thanks all for the explanations! That makes it much clearer!

1 Like