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.5

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

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
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
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement