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?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #blog-post
- #ai
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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
What about pattern-matching in functions? Or that is a totally different strategy, i.e. not an Elixir-way?
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
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
amounttype.OvermindDL1
You can match on the struct type as well.
axelson
But you can’t match on multiple struct types at once, correct?
How would you combine these into one clause?
blatyo
Not the prettiest, but:
But you could turn part of that into a guard.
lessless
That’s what I’m talking about. It feels that this can be solved in a more straightforward manner.
blatyo
If you’re on the newest erlang you could do.
dom
I realize it’s side-stepping the question, but this really sounds more like a use case for a record:
{:currency, “HKD”, amount}. Thenadd,equals?, etc. are trivial to write.What are you trying to achieve with the structs?