tino415

tino415

Hello,

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:

Example:

defmodule Example do
  @before_compile TsAccess
  use TypedStruct

  typedstruct do
    field(:name, :string)
  end
end

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?

Showing Posts 1 to 9

LukeWood

LukeWood

Will these throw an error if given a map?

Will these preserve the struct type when setting a value?

If so seems great!

tino415

tino415 OP

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

  2. Yes

Thank you for feedback

tino415

tino415 OP

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

LukeWood

LukeWood

Exceptional! Thanks, this is awesome!

tino415

tino415 OP

Hello again, I just published version 0.5.0 in which typedstruct is not neceseary anymore and also explicit mode was added where you can define getters explicitly:

defmodule Example do
  use TsAccess, explicit: true
  defstruct [
    :accesible,
    :gettable,
    :settable
  ]

  defaccessor :accessible
 
  defgetter :gettable

  defsetter :settable
end

All macros support also setting of attribute type:

defsetter :name, String.t()

In case of nonexplicit mode without typed struct, types for specs can by set using @types module attribute:

defmodule Example do
  use TsAccess

  @types [
    name: String.t()
  ]
  defstruct [
    :name
  ]
end

Hope someone find this usable :slight_smile:
tino415

tino415 OP

0.6.0 I also added lenses generator (base on elixir Access module) generated functions looks like:

get_in(example, [Example.name_lens()])
tino415

tino415 OP

After some sleep I realized that I don’t need postfix _lens, so now lenses are just field functions without arguments a.k.a get_in(example, [Example.name]) function with postfix _lens are generated with deprecation warning. Also any other ideas how further improve it? I have several:

  • in implicit mode add exclude, only to using
  • add prefix, postfix or callback to generate function names
  • explicit deflens, but this is little bit complicated since I don’t know how to check if field exists
  • add alias option to defsetter, defgetter and defaccesor
  • add onli, exclud, onli to defaccessor (in case of explicit lens, to choose what should by generated)

Feedback is welcomed :slight_smile:

OvermindDL1

OvermindDL1

Ooo I really like how you setup the lenses!

tino415

tino415 OP

Released 1.0.0 with removed deprecated _lens functions and added @doc to generated functions :slight_smile:

— All posts loaded —

Where Next? Top

Trending in Announcing Top

woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
woylie
I released Doggo, a collection of unstyled Phoenix components. https://github.com/woylie/doggo Features Unstyled Phoenix components....
New
GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
ahamez
Hi everyone, I’ve been working on this protobuf library for 3 years. We use it in the company I work for, EasyMile, to communicate with ...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews