binarypaladin
I’m working on a project right now that makes heavy, heavy use of Decimal. I just hit bug today where >= was used instead of Decimal.compare/2 and it’s not the first time I’ve seen this happen.
Additionally, Decimal has some nice convenience methods (as Java-esque as they are) such as eq?/2 and gt?/2. This is nicer than having to drop into compare/2.
It seems like protocols could totally make operator overloading a thing. I would obviously really like to just be able to do %Decimal{} + %Decimal{} and have it work like Decimal.add/2. And, coming from a Ruby background, I’m, used to overloading a lot.
These are just functions, yes? I can pipe with Kernel.+/2 and Kernel.==/2.
Having said all that, this seems quite deliberate and I feel like with as much as I have read I would have come across the answer but… I don’t know. And either my Googling skills are poor or I’m asking the wrong question.
Why doesn’t Elixir support standard operator overloading?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 10 of 20 Posts
sodapopcan
You could do something like this:
For me, the lack of operator overloading is part of what makes Elixir a good dynamic language as it still cares about types. It’s also part of (all of?) what is enabling the possibility of strong arrows (if we get some static typing). If you looked at a language like OCaml it goes even further.
+only adds ints, you need to use+.to add floats! And you must explicitly cast one side if you want to add a float to and int.I don’t know if this is the exact reason Elixir doesn’t overload, though.
D4no0
I think the reason is not to make unreadable code, the example you provided shows clearly how someone can get lost when he will want to use + with non decimal values.
In this case matching or a guard would be a better solution if that is possible, I didn’t work with operators before in elixir so I have no idea about the potential limitations.
binarypaladin
That’s an interesting example. You could contextually overload (almost like a Ruby refinement) in the current module.
With that said, I can see the argument for arithmetic operators. I almost wish I hadn’t included it because what was lost was not that potential convenience.
The pain point isn’t really arithmetic.
Decimal.add/2will chuck anArithmeticErrorif you try and just use+. So I might gripe, but I can easily fix.The real problem comes with equality operators. They won’t chuck an exception. They’ll often just be flat out wrong. (Well, “wrong” in terms of what I intended when comparing structs, because it compares just as it would a map because… it’s a map.) So instead of getting a “you can’t do this” I get something that can fail in subtle ways.
binarypaladin
Awesome. A buddy of mine actually just pointed me to this commit.
It is indeed a conscious decision related to readability:
I guess in that case, just like structs don’t work by default with the
Accessbehaviour, it would sure be nice if they blew up on equality operators if they hadn’t implementedcompare/2although I suspect there are issues with that.D4no0
You could replace
==operator with a custom one that would make the necessary checks, at compile-time/runtime overhead if that is critical for business.billylanchantin
CompareChain might help with some of your use cases (shameless plug: I’m the author). I had similar headaches working with
DateTimestructs. It’s easier to read code like:left <= rightvsDateTime.compare(left, right) != :gt.CompareChain is a sort of middle ground where you can write this:
It’s not perfect, but I find that it helps.
binarypaladin
I’ll take a look!
All things date and time also fall under this same pain point.
sodapopcan
Sorry I’m a bit lost in your wording re: unreadable code but I’m talking more how many languages overload
+to work with several data types, like:1 + 1,"Hello" + "!", and[1, 2, 3] + [4]whereas Elixir has its own operators. But of course:I always forget about this when thinking/talking about this and ya, that always gets me as I wish there were strict “math” versions. So, ya, it’s best to use guards and pattern matching when working with equality.
Note that is says “generally”
I think if you have a lot of often-edited modules in your business domain that deal purely with decimals, it would be totally fine to write a macro like my example. You can also override per function scope too! Just don’t do it project-wide. There are very good libraries out there that overload these operators, like Image (see the
Mathmodule] and Pathex (allows you to use/as a path separator). But if it’s for some simple convenience for a module or two that don’t get changed much, I personally wouldn’t bother. It just makes it confusing later on.sodapopcan
This is a really nice middle ground. I like it.
binarypaladin
I come from a Ruby background so… I use and abuse overloading, lol. I’m okay with a decision one way or another. At least it isn’t Java’s “you can’t overload but we selectively have one overload exception for concatenating strings.”