Syntax error before: '%{}'

defmodule Sandbox do

 def fast_cars_index do
  fast_cars =
    %{color: "Red", make: "Mclaren", mileage: 15641.469},
    %{color: "Blue", make: "Ferrari", mileage: 120012.481},
    %{color: "Red", make: "Ferrari", mileage: 29831.021},
    %{color: "Black", make: "Ferrari", mileage: 24030.674},
    %{color: "Cobalt", make: "Ferrari", mileage: 412.811},
    %{color: "Blue", make: "Koenigsegg", mileage: 250.762},
    %{color: "Cobalt", make: "Koenigsegg", mileage: 1297.76},
    %{color: "Titanium", make: "Koenigsegg", mileage: 5360.336},
    %{color: "Blue", make: "Maserati", mileage: 255.78}
    fn %{color: color, make: make, mileage: mileage}
        make: make,
        inserted_at: DateTime.truncate(DateTime.utc_now, :second),
        updated_at: DateTime.truncate(DateTime.utc_now, :second),
          color: color,
          mileage: mileage


iex()> recompile        
Compiling 1 file (.ex)

== Compilation error in file lib/Sandbox.ex ==
** (SyntaxError) lib/Sandbox.ex:25: syntax error before: '%{}'
    (elixir 1.10.3) lib/kernel/parallel_compiler.ex:304: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/7
** (exit) shutdown: 1
    (mix 1.10.3) lib/mix/tasks/compile.all.ex:62: Mix.Tasks.Compile.All.do_compile/4
    (mix 1.10.3) lib/mix/tasks/compile.all.ex:27: anonymous fn/2 in
    (mix 1.10.3) lib/mix/tasks/compile.all.ex:43: Mix.Tasks.Compile.All.with_logger_app/2
    (mix 1.10.3) lib/mix/task.ex:330: Mix.Task.run_task/3
    (mix 1.10.3) lib/mix/tasks/compile.ex:96:
    (mix 1.10.3) lib/mix/task.ex:330: Mix.Task.run_task/3
    (iex 1.10.3) lib/iex/helpers.ex:104: IEx.Helpers.recompile/1

As you can see, I’m trying to create a list of maps within maps. What’s the correct syntax for this? Thanks for you assistance! :slight_smile:

There’s no key for the %{color: color, mileage: mileage } value in the map, maybe you mean something like car: %{color: color, mileage: mileage }?


I don’t see a literal %{} anywhere in the version of Sandbox in your post; it’s going to be tricky to diagnose the syntax error without the exact code that’s giving the error.

The error suites the code. %{} is the special form that creates the map. In the AST it looks like {:%{}, _, key: :value}

1 Like

The problem is here
make: make,
inserted_at: DateTime.truncate(DateTime.utc_now, :second),
updated_at: DateTime.truncate(DateTime.utc_now, :second),
%{ <====
** color: color,**
** mileage: mileage**
** }**


That’s what was missing, thanks so much! :slight_smile:

That is indeed confusing. I wonder if the error message could be improved to show only code that actually exists in the file (exactly as shown in the error message).