lessless

lessless

For example I have multiple currencies

dollar.ex

defmodule Multicurrency.Currency.Dollar do
  @enforce_keys [:amount]
  defstruct [:amount]

  @type t :: %__MODULE__{
    amount: integer()
  }

  @spec new(number()) :: Multicurrency.Currency.Dollar.t()
  def new(amount) do
    %__MODULE__{amount: amount}
  end
end

franc.ex

defmodule Multicurrency.Currency.Franc do
  @enforce_keys [:amount]
  defstruct [:amount]

  @type t :: %__MODULE__{
    amount: integer()
  }

  @spec new(number()) :: Multicurrency.Currency.Franc.t()
  def new(amount) do
    %__MODULE__{amount: amount}
  end
end

They are basically the same and I wonder if it’s possible to have some kind of a generic struct or a protocol for structs that will define common keys and allow to pattern match on the instances of that struct:

defmodule Multicurrency.Currency do
  @enforce_keys [:amount]
  defstruct [:amount]

  @type t :: %__MODULE__{
    amount: integer()
  }

end
defmodule Multicurrency.Currency.Franc do
  @impl Multicurrency.Currency

  @spec new(number()) :: Multicurrency.Currency.t()
  def new(amount) do
    %__MODULE__{amount: amount}
  end
end
def equals?(Multicurrency.Currency{amount: a1},  Multicurrency.Currency{amount: a2}) do
    a1 == a2
  end

Does it even make sense?

Showing Posts 1 to 10

OvermindDL1

OvermindDL1

You can with the built-in Protocol if you handle all currency types within your singular protocol implementation or double-dispatch out to another protocol for the specific currency type (which should be fine).

If you truly want to protocol dispatch over an instance binding (I.E. full match) then the built-in Protocol cannot do that but ProtocolEx can (and it can be faster than Elixir protocols as well). But I don’t recommend it until you actually Need proper match semantics.

lessless

lessless OP

What about pattern-matching in functions? Or that is a totally different strategy, i.e. not an Elixir-way?

OvermindDL1

OvermindDL1

That’s what I meant in my first option, pattern match in the functions of the implementation (or the direct calls, however you are doing it). This is perfectly fine if you don’t need it to be extensible via plugins or so.

axelson

axelson

Scenic Core Team

Yeah but you can only pattern match on the map keys, so you can’t be certain you’re getting a currency, you may be just getting a struct that happens to have an amount type.

OvermindDL1

OvermindDL1

You can match on the struct type as well.

axelson

axelson

Scenic Core Team

But you can’t match on multiple struct types at once, correct?

How would you combine these into one clause?

def add(%Multicurrency.Currency.Franc{} = currency) do ... end

def add(%Multicurrency.Currency.Dollar{} = currency) do ... end
blatyo

blatyo

Conduit Core Team

Not the prettiest, but:

def add(%currency_type{} = currency) 
when currency_type in [Multicurrency.Currency.Franc, Multicurrency.Currency.Dollar] do 
end

But you could turn part of that into a guard.

lessless

lessless OP

That’s what I’m talking about. It feels that this can be solved in a more straightforward manner.

blatyo

blatyo

Conduit Core Team

If you’re on the newest erlang you could do.

defguard is_currency(currency) 
when :erlang.map_get(:__struct__, currency) in [Multicurrency.Currency.Franc, Multicurrency.Currency.Dollar]

def add(currency) when is_currency(currency) do
end
dom

dom

I realize it’s side-stepping the question, but this really sounds more like a use case for a record: {:currency, “HKD”, amount}. Then add, equals?, etc. are trivial to write.

What are you trying to achieve with the structs?

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews