coen.bakker

coen.bakker

Getting `unexpected reserved word: end` when returning string from function

When I define successive functions that return a string I get the error:

unexpected reserved word: end

    HINT: it looks like the "end" on line 3 does not have a matching "do" defined before it

    (elixir 1.14.0) lib/kernel/parallel_compiler.ex:346: anonymous fn/5 in Kernel.ParallelCompiler.spawn_workers/7

The error refers to line 3 of this function:

defmodule ModuleA do
  @start "@"
  @end "#"

  defmacro start, do: @start
  defmacro end, do: @end
end

However, the error also occurs in a simpler setup, like:

defmodule ModuleB do
  def start, do: "@"
  def end, do: "#"
end

The error does not occur in a module with only one function that returns a string.

defmodule ModuleC do
  def start, do: "@"
end

I reckon it something small I am overlooking, but it bugs me that I don’t understand why it fails. Any ideas?

Most Liked

patrickdm

patrickdm

Because end is a reserved word?
from: Syntax reference — Elixir v1.12.3

Reserved words
These are the reserved words in the Elixir language. They are detailed throughout this guide but summed up here for convenience:

true, false, nil - used as atoms
when, and, or, not, in - used as operators
fn - used for anonymous function definitions
do, end, catch, rescue, after, else - used in do/end blocks

Interactive Elixir (1.14.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> defmodule ModuleB do
...(1)> def start, do: "@"
...(1)> def end, do: "f" 
** (SyntaxError) iex:3:8: syntax error before: ','
    |
  3 | def end, do: "f"
    |        ^
    (iex 1.14.2) lib/iex/evaluator.ex:292: IEx.Evaluator.parse_eval_inspect/3
    (iex 1.14.2) lib/iex/evaluator.ex:187: IEx.Evaluator.loop/1
    (iex 1.14.2) lib/iex/evaluator.ex:32: IEx.Evaluator.init/4
    (stdlib 4.0.1) proc_lib.erl:240: :proc_lib.init_p_do_apply/3
benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Yeah as @patrickdm notes end is reserved, I would pick a different function / macro name. Even if you can successfully construct a function / macro named end it’s going to be very difficult to call.

coen.bakker

coen.bakker

Oh boy. I knew that. Must be tired. :dotted_line_face:

Makes all sense now. Thank you!

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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

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
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

We're in Beta

About us Mission Statement