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!

Where Next?

Popular in Questions Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
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
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

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
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New

We're in Beta

About us Mission Statement