snunezcr

snunezcr

Defining operations using protocols

I need to define a protocol for a scientific application based on quantities of various types. I want to use protocols since they abstract my code nicely. Is there a valid way to re-define math operators in protocols, e.g.

defprotocol PhysQuant do
@doc “Compute arithmetic operations”
def qa + qb
def qa - qb
def qa * qb
def qa / qb
def qa ^ qb
def exp(quantity)
def sin(quantity)
def cos(quantity)
def tan(quantity)
end

Thanks in advance.

First Post!

dimitarvp

dimitarvp

Please use triple backtick blocks to format code: Markdown cheat sheet.

And no, Erlang and Elixir do not allow overriding of operators per se. You are much better off using function names like add, sub, mul, div, mod etc.

Most Liked

ityonemo

ityonemo

It’s possible. You can un-import from Kernel and re-import. For example:

https://github.com/ityonemo/annex/blob/experimental/test/experimental/elixir_tensor_test.exs

However, it’s somewhat discouraged.

wojtekmach

wojtekmach

Hex Core Team

Agreed. Another approach is to limit operator overrides to a macro call so that outside of it things continue to work as usual. Here’s a proof-of-concept I did for Decimal [1] a while ago:

iex> require DecimalEval
iex> a = 1
iex> b = 2
iex> DecimalEval.eval (a + b) * "3.0"
#Decimal<9.0>

[1] GitHub - wojtekmach/decimal_eval · GitHub

snunezcr

snunezcr

Thank you. The code in your link clarified many other questions I already had.

Last Post!

ityonemo

ityonemo

just keep in mind that 1) the changes are lexically scoped. That’s a good thing ™ since it contains the statefulness of potentially confusing conventions and 2) the changes are strictly opt-in, you can’t enforce it globally (AFAIK) and 3) they’re not composable. If someone else has a library that does that you can’t use it in the same code block as yours.

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
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

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54092 488
New
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
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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

We're in Beta

About us Mission Statement