witchcrafters/algae

Documentation Example for Overwrite Constructor shows syntax error

hariroshan opened this issue · 1 comments

Overwrite Constructor
The new constructor function may be overwritten.

defmodule Constant do
  defdata :: fun()

  def new(value), do: %Constant{constant: fn _ -> value end}
end

fourty_two = Constant.new(42)
fourty_two.constant.(33)
#=> 42

I copy pasted this code sample. Elixir is giving me this following error.

== Compilation error in file lib/core/data.ex ==
** (CompileError) lib/core/data.ex:15: misplaced operator ::/2

The :: operator is typically used in bitstrings to specify types and sizes of segments:

    <<size::32-integer, letter::utf8, rest::binary>>

It is also used in typespecs, such as @type and @spec, to describe inputs and outputs

** (exit) shutdown: 1
    (mix) lib/mix/tasks/compile.ex:102: Mix.Tasks.Compile.run/1
    (mix) lib/mix/task.ex:331: Mix.Task.run_task/3
    (iex) lib/iex/helpers.ex:102: IEx.Helpers.recompile/1

could you please check this ?

if I define it without the " :: " operator, there are no errors

defmodule Constant do
  defdata fun()

  def new(value), do: %Constant{constant: fn _ -> value end}
end

Indeed, with no field names you need to remove the ::. This is in a few places in the README, but I guess it got typoed in the docs. The reason for the syntax inconsistency was the Elixir parser was having issues with the :: when there was no first argument to the macro this form.

Thanks for the report :)