@enforce_keys and Kernel.struct

First of all

Happy New Year everyone

Disclaimer I did not find an issue on Github or a discussion here on this topic, if I missed it, my appologies

one of my resolutions have been to work on EarmarkParser, … again :blush: and during my refactorings I stumbled about a puzzling behavior

defmodule LP2.Parser.ListInfo do                                                                                                                                                                      
  use LP2.Types                                                                                                                                                                                       
                                                                                                                                                                                                      
  @enforce_keys [:bullet, :list_indent, :loose]                                                                                                                                                       
  defstruct bullet: nil, list_indent: nil, loose: false                                                                                                                                               
                                                                                                                                                                                                      
  @type t :: %__MODULE__{bullet: binary(), list_indent: non_neg_integer(), loose: boolean()}                                                                                                                                                                                                                                                   
end                                                       

please note the @enforce_keys, I verified that that works and then found, quite puzzled, that it does not :confused:

iex(1)> alias LP2.Parser.ListInfo
LP2.Parser.ListInfo
iex(2)> struct(ListInfo)
%LP2.Parser.ListInfo{bullet: nil, list_indent: nil, loose: false}

then I tried this

 iex(3)> %ListInfo{}
** (ArgumentError) the following keys must also be given when building struct LP2.Parser.ListInfo: [:bullet, :list_indent, :loose]
    (lp2 0.1.0) expanding struct: LP2.Parser.ListInfo.__struct__/1
    iex:3: (file)

which is good, of course, now my questions are

  • is the usage of struct discouraged?
  • if not, should the behavior of ignoring @enforce_keys not be considered as a bug?

KR
Robert