Is `@__struct__` an implementation detail or a public API?

60 views
Skip to first unread message

w...@resilia.nl

unread,
Nov 19, 2021, 5:00:40 PM11/19/21
to elixir-lang-core
Hi all!

I recently needed to check what fields for a struct were defined (if any) for a particular module, at compile-time (potentially in the body of that module, i.e. while the module was still 'open').
(for the curious, it's part of code that builds a runtime type-check from a typespec, see here)

There was no information in Elixir's documentation about this. Looking at the source of `defstruct` we however find that it defines a `@__struct__` module attribute (which is then later on used to implement the introspection method `__struct__/0`).

Now the question is: Is it OK for libraries to depend on `@__struct__` or should it be considered an implementation detail which is subject to change?

-If it's OK: Should we maybe add a sentence to the module documentation of `Module`? Or the documentation of `Kernel.SpecialForms.%/2`?
- If it's not: Is there a 'proper' way to obtain this information for an 'open' module instead?

Thank you all,

~Marten/Qqwy

eksperimental

unread,
Nov 19, 2021, 5:10:08 PM11/19/21
to elixir-l...@googlegroups.com
Hi Marten,
You will find this PR interesting
https://github.com/elixir-lang/elixir/issues/10352

Cheers,
- Eks

On Fri, 19 Nov 2021 14:00:40 -0800 (PST)
"w...@resilia.nl" <w...@resilia.nl> wrote:

> Hi all!
>
> I recently needed to check what fields for a struct were defined (if
> any) for a particular module, at compile-time (potentially in the
> body of that module, i.e. while the module was still 'open').
> *(for the curious, it's part of code that builds a runtime type-check
> from a typespec, see here
> <https://github.com/Qqwy/elixir-type_check/blob/21d5de5a1979ebd10d0f30818b6e837062027269/lib/type_check/internals/pre_expander.ex#L265-L270>)*
>
> There was no information in Elixir's documentation about this.
> Looking at the source of `defstruct`
> <https://github.com/elixir-lang/elixir/blob/79cd891eb86ecb7654a7acdb63769cfdd950a5c0/lib/elixir/lib/kernel.ex#L5037-L5046>

eksperimental

unread,
Nov 19, 2021, 5:11:06 PM11/19/21
to elixir-l...@googlegroups.com

Sorry, that was the issue.
This is the actual PR https://github.com/elixir-lang/elixir/pull/10354
On Fri, 19 Nov 2021 17:09:55 -0500
"'eksperimental' via elixir-lang-core"

José Valim

unread,
Nov 19, 2021, 5:14:22 PM11/19/21
to elixir-l...@googlegroups.com
There is Macro.struct!(module, env), which will expand the struct for a given module accordingly and most likely what you would want to use.

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/619820bd.1c69fb81.8c1b5.36d4SMTPIN_ADDED_MISSING%40gmr-mx.google.com.

w...@resilia.nl

unread,
Nov 19, 2021, 6:07:16 PM11/19/21
to elixir-lang-core
Thank you both!
`Macro.struct!/2` is perfect for my needs.

~Marten/Qqwy
Reply all
Reply to author
Forward
0 new messages