IvanR

IvanR

Dear community,

There are lovely TypedStruct by @ejpcmac and TypedEctoSchema by @bamorim libraries to define structs in combination with t() types.

Using them, I missed the validation of default values conformance to t(). That validation seems good because initial default values defining the app’s state can drift from the type definition later as the code evolves. That, in turn, can confuse the local thinking about what’s correctly defined the value or the type?

To address this, I’ve added the compile-time defaults validation feature to the Domo library. That works for any struct that has a t() type defined. And can be activated by adding use Domo like the following:

defmodule Order do
  use TypedStruct
  use Domo

  typedstruct enforce: true do
    field :id, non_neg_integer()
    field :items, [String.t()], default: []
  end
end

That gives the compile-time error highlighting the issue immediately :smile:

Compiling 1 file (.ex)
Domo is compiling type ensurer for 1 module (.ex)

== Compilation error in file lib/functional_core_struct.ex:55 ==
** A default value given via defstruct/1 in Order module mismatches the type.
"Invalid value nil for field :id of %Order{}. Expected the value matching the non_neg_integer() type."

And adding default: 1 option to field :id or changing its type to non_neg_integer() | nil leads to successful compilation.

How do you see it can be worthful to your projects?

Showing Posts 1 to 5

dimitarvp

dimitarvp

This is extremely useful. I’d use the hell out of it!

ejpcmac

ejpcmac

Hello @IvanR, thank you for mentioning me here, you’ve just made me aware of Domo :slight_smile: This seems very neat!

I am not writing much code in Elixir these times, doing mainly embedded stuff in Rust at home (and C / Python at work), but be sure I’ll use it on my next projects using Elixir (certainly some Nerves firmware communicating with other embedded systems written in Rust) :slight_smile:

Validating the default values was on my roadmap, but since you’ve solved it in an elegant way and I
want TypedStruct to be thin, can I mention it in the README / docs as a solution for this?

wolf4earth

wolf4earth

FYI, if you’re interested to learn more about Domo, we’ve recently had @IvanR as a guest in ElixirMix where we talked about Domo:

IvanR

IvanR OP

Hello @ejpcmac,

Thanks for your reply. As the proverb says, the appropriate tool is to be used for the appropriate problem. Sometimes, other languages fit better, and it’d be super good if you use Elixir/Nerves on the other project! :slightly_smiling_face:

I’m glad that you liked the library! :slightly_smiling_face: I was inspired a lot by TypedStruct working on it. And I’ll be delighted if you mention the Domo solution for the default checks in the README/docs.

IvanR

IvanR OP

Thanks a lot for having me at ElixirMix back then. It was a very welcoming and good atmosphere during the recording, which I liked very much! I’ll be glad to come again :slightly_smiling_face:

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 91898 914
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
byu
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project. My initial shotgu...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
matt-savvy
Is there a word for the ~> symbol used in Version strings? Do you also just call it a Squiggle Arrow™ ?!
New

Other Trending Topics Top

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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews