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

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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
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
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
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
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New

Other popular topics Top

greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
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
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
I would like to know what is the best IDE for elixir development?
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

We're in Beta

About us Mission Statement