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

earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
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
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
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
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement