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

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
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
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
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement