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
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
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
Thanks all for the explanations! That makes it much clearer!
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








