typed_struct icon indicating copy to clipboard operation
typed_struct copied to clipboard

default value not checked if the typespec is missing

Open meox opened this issue 8 months ago • 2 comments

I have a typedstruct defined in this way:

defmodule Msg do
    use TypedStruct

    typedstruct do
      field(:version, default: 1)
      field(:note, String.t(), default: "")
    end
end

If I default construct the record in this way: %Msg{} I got

%Msg{version: nil, note: ""}

instead of

%Msg{version: 1, note: ""}

Specifying the typespec everything works as expected.

meox avatar Nov 27 '23 17:11 meox

Consider this plugin

  • typed_struct_ctor – Adds validating (new and from) constructor functions.

    Try the macro out in real time without having to install or write any of your own code.
    All you need is a running instance of Livebook

    Run in Livebook

leggebroten avatar Dec 06 '23 21:12 leggebroten

Hello @meox, this is indeed a bug: we should check that the second argument to field/2 is a type.

ejpcmac avatar Dec 29 '23 19:12 ejpcmac