[Proposal] Add defines_struct?/1

40 views
Skip to first unread message

Yordis Prieto

unread,
Feb 12, 2022, 4:30:32 PM2/12/22
to elixir-lang-core
Working in macro land lately where I would like to validate if the module is also a struct forces me to understand more technical things (which is good), where I wish more functions are defined in the Core where I can focus on the intention, especially that checking __struct__ (or any double-underscore prefix + suffix) feels too close to internals.

def defines_struct?(module) do function_exported?(module, :__struct__, 0)
end Better naming welcome. Also, maybe do kind of the same around other internal things with the same intention.

P.S: Credit to Jon R, thank you.

Yordis Prieto

unread,
Feb 12, 2022, 5:09:05 PM2/12/22
to elixir-lang-core

It seems that would be more like

def defines_struct?(module) do
  :functions
  |> module.__info__()
  |> Keyword.get(:__struct__)
  |> Kernel.!=(nil)
end
Reply all
Reply to author
Forward
0 new messages