Fl4m3Ph03n1x
Background
Let’s say I have a TypedStruct that has some common fields:
typedstruct do
@typedoc "A product."
field(:item_id, String.t())
field(:name, String.t())
field(:brand, String.t())
end
This is a product and because products can go on sale, I have another TypedStruct:
typedstruct do
@typedoc "A sale."
field(:item_id, String.t())
field(:name, String.t())
field(:brand, String.t())
field(:price, number)
field(:discount, boolean)
end
Issue
Now, you are probably seeing there is some duplication here. Since a sale is a product with extra info, some of the fields are replicated.
A first attempt at a solution could be something like this:
typedstruct do
@typedoc "A sale."
field(:product, Product.t())
field(:price, number)
field(:discount, boolean)
end
However, my use case does not allow for this. I must have a struct with exactly the same fields as Product.t() and a few extra. I cannot simply smash a product field into my sales struct.
Question
- Is there a way to compose types using TypedStruct (or normal structs?)
- If not, what do you think would be the solution with the least amount of copy/pasting for this issue?
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New
Other Trending Topics
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 6 of 6 Posts
tcoopman
Are you saying a sale contains a product (1)? Or is a sale something completely different from a product(2)?
If (2): They’re not the same so don’t try to do as if they’re the same.
In case of (1): I’m really interested as for why you can’t do what you proposed with product as part of sale?
Fl4m3Ph03n1x
I would go for (1). A sale is not a product, but contains one.
I can’t follow the proposed solution because the generated Json file would not be compatible with the API I am integrating with.
tcoopman
I think the cleanest solution would be to write a custom encoder for the product: Jason.Encoder — jason v1.4.5
Although I would not mind this kind of duplication either.
It really depends on the domain and what sale and products are exactly.
LostKobrakai
That sounds like you‘re missing a translation layer between your internal data model and the outside world.
Fl4m3Ph03n1x
Yes, that is the thing I am trying to build
Would you go for duplication, for an implementation of the Jason encode protocol, or something else?
LostKobrakai
I’d go for the simplest to change option for things outside of your control, which would likely be duplication. Don’t try to abstract something other people are in charge off.