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

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

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
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
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement