Onor.io

Onor.io

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?

Marked As Solved

al2o3cr

al2o3cr

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
    |                      ^

Also Liked

LostKobrakai

LostKobrakai

sodapopcan

sodapopcan

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()
Onor.io

Onor.io

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

Last Post!

Onor.io

Onor.io

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

Where Next?

Popular in Questions Top

hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement