TsAccess - Getters/Setters generator for TypedStruct


recently I started using TypedStruct and also for quite some time I have problem with typos when using structs in elixir, mainly that struct.fiewld_name is not reported during compile time. So I was playing with idea to create plugin for TypedStruct that would generate getter and setter functions for struct. Good thing is, that when I do typo in function name, code don’t compile, another good thing is that it also generate typespecs, so I assume that dialyzer should report when I try to pass wrong type to setter (untested). I used @before_compile and module attributes that TypedStruct is adding to module:


defmodule Example do
  @before_compile TsAccess
  use TypedStruct

  typedstruct do
    field(:name, :string)

iex> Example.name(%Example{}, "John Doe")
%Example{name: "John Doe"}

iex> Example.name(%Example{name: "John Doe"})
"John Doe"

I’m also thinking to do another library (or as another feature to this one) that would generate lenses

What do you think?


Will these throw an error if given a map?

Will these preserve the struct type when setting a value?

If so seems great!

  1. Yes (it did not, but great idea so I added it :D)

  2. Yes

Thank you for feedback

1 Like

Since it is mi first try at doing library for elixir, CR and API critique is welcomed :slight_smile:

Exceptional! Thanks, this is awesome!