Best way to avoid duplication in struct definition?

As the title says, can someone advises on the best way to remove the duplication in the code snippet below.
I am referring to the field list. By the way is there a way to retrieve that field list from any struct - did not find anything in the doc.

Many thanks.
S

  defmodule Company do
    @type t :: %__MODULE__{
      id:     integer,
      name:   String.t,
      domain: String.t,
      rating: float
    }

    @enforce_keys [:id, :name, :domain, :rating]
    defstruct [:id, :name, :domain, :rating]
  end
1 Like
defmodule Company do
    @enforce_keys [:id, :name, :domain, :rating]
    defstruct @enforce_keys
end
defmodule Company do

    @enforce_keys [:id, :name]
    @other_keys [:domain, :rating]
    @company_keys @enforce_keys ++ @other_keys

    defstruct @company_keys
end
5 Likes

Thank you, this is neat.