typed_struct
typed_struct copied to clipboard
default value not checked if the typespec is missing
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.
Consider this plugin
-
typed_struct_ctor– Adds validating (newandfrom) 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
Hello @meox, this is indeed a bug: we should check that the second argument to field/2 is a type.