Fl4m3Ph03n1x

Fl4m3Ph03n1x

Composing Types with TypedStruct?

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?

Marked As Solved

LostKobrakai

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.

Also Liked

LostKobrakai

LostKobrakai

That sounds like you‘re missing a translation layer between your internal data model and the outside world.

tcoopman

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?

tcoopman

tcoopman

I think the cleanest solution would be to write a custom encoder for the product: Jason.Encoder — jason v1.4.0

Although I would not mind this kind of duplication either.

It really depends on the domain and what sale and products are exactly.

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement