I am getting a weird error and I don’t know why it is wrong
Legacy warning:
lib/strategy/strategy.ex:33: Invalid type specification for function 'Elixir.Strategy':new/1.
The success typing is 'Elixir.Strategy':new(_) -> #{'__struct__':='Elixir.Strategy', 'id':=<<_:288>>, 'name':=_, 'type':='static'}
But the spec is 'Elixir.Strategy':new('Elixir.StringHelpers':word()) -> 'Elixir.Strategy'
The return types do not overlap
defmodule Strategy.StrategyId do
@type t :: %__MODULE__{
strategy_id: Ecto.UUID.t()
}
defstruct [strategy_id: nil]
end
defmodule Strategy do
alias Strategy.StrategyId
alias __MODULE__
@type strategy_type() :: :dynamic | :static
@type t() :: %__MODULE__{
id: StrategyId.t(),
name: String.t(),
type: strategy_type(),
}
defstruct [name: "", id: Ecto.UUID.generate(), type: :static]
@spec new(String.t()) :: __MODULE__.t()
def new(word) do
id = Ecto.UUID.generate()
%__MODULE__{id: id, name: word}
end
end
Side note regarding the id: Ecto.UUID.generate() default, please note this will generate a random default UUID at compile time, reused for all instances at runtime, which might not be what you expect.
If you mean this key to always be set when building the struct, you could use @enforce_keys instead of having to define a default.
+1 this warning; here’s an example of where a similar “default defined at the wrong time” issue caused a startup a lot of headaches:
That issue was harder to troubleshoot because the “compilation” happens later in Python, so each instance of the application had a distinct always-the-same default ID.
Thanks
Figured about Ecto.UUID.generate() so my new() sets a fresh one each time.
Still dont’ know what was wrong with the previous one… but I got the patternt