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

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

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
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
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
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement