witchcrafters/algae

Unable to overwrite constructor on defdata inside defsum

OvermindDL1 opened this issue · 1 comments

Unable to overwrite constructor on defdata inside defsum.

Precisely that, as from the examples with an overwrite added as per the defdata section:

defmodule Pet do
  defsum do
    defdata Cat do
      name :: String.t()
      claw_sharpness :: String.t()

      def new(name, clawness) when byte_size(name)>=3, do: %Cat{name: value, claw_sharpness: clawness}
    end

    defdata Dog do
      name :: String.t()
      bark_loudness :: non_neg_integer()

      def new(name, barkness) when byte_size(name)>=3, do: %Dog{name: value, bark_loudness: barkness}
    end
  end
end

Fails to compile with a message of like: (though from my code):

** (FunctionClauseError) no function clause matching in Algae.Internal.normalize_elements/1
    lib/algae/internal.ex:149: Algae.Internal.normalize_elements({:def, [line: 28], [{:when, [line: 28], [{:new, [line: 28], [{:type, [line: 28], nil}, {:\\, [line: 28], [{:meta, [line: 28], nil}, {:%{}, [line: 28], []}]}]}, {:and, [line: 28], [{:is_map, [line: 28], [{:meta, [line: 28], nil}]}, {:in, [line: 28], [{:type, [line: 28], nil}, {:@, [line: 28], [{:const_types, [line: 28], nil}]}]}]}]}, [do: {:%, [line: 28], [{:__aliases__, [counter: 0, line: 28], [:TConst]}, {:%{}, [line: 28], [type: {:type, [line: 28], nil}, meta: {:meta, [line: 28], nil}]}]}]]})
    lib/algae/internal.ex:135: anonymous fn/2 in Algae.Internal.module_elements/1
    (elixir) lib/list.ex:200: List."-foldr/3-lists^foldr/2-0-"/3
    lib/algae/internal.ex:11: Algae.Internal.data_ast/1
    expanding macro: Algae.defdata/2
    lib/ml_elixir/types.ex:25: MlElixir.Types (module)
    expanding macro: Algae.defsum/1
    lib/ml_elixir/types.ex:19: MlElixir.Types (module)

We can define it outside but then that just puts a struct in a struct, which is noisy indirection.

It would be also handy to support module attributes to support @moduledoc and @derive at least.