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?

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
3 Likes

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.

1 Like

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

Makes all sense now. Thank you!

1 Like