What's the best way to define a module-local struct?

What’s the best/most succinct way to define a struct that is only used inside of one module? I sometimes have structures that I want to be more explicit than a map but that have no use elsewhere.

Is there an accepted solution to this problem in the community?

There is no way to do that. Structs are a module based definition for specialized maps and just like the defining modules themselfs are globally accessible so are their struct features.

The best way I know, which you may well also know, is the submodule syntax with @moduledoc false:

defmodule Foo do
  defmodule Bar do
    @moduledoc false

    defstruct [:one, :two, :three]
  end
end

Elixir doesn’t have private modules so @moduledoc false is the next best thing (I actually don’t mind this).

Note: It is not available at compile time (at least not that I know of), ie, you can’t use it in the module body. Inside of functions is fine.

1 Like

Both answers so far address limiting external accessibility, but in my literal reading of the OP I don’t see that concern. So my naive answer is: just do a defstruct in the module which will be the sole consumer of the struct.

But I must be reading it wrong.

Yeah that’s an option, but if that’s the only one I’d rather keep it a Map, as my hunch says that no description might be better than a misleading description.