elchemy
elchemy copied to clipboard
Math operations might throw errors
Right now divide by 0 on Ints returns 0. Divide by 0 on floats returns NaN.
ANY MATH OPERATIONS ON NAN THROW ERROR.
It'd be nice to find a safe way to express 0 division consistently
Right now divide by 0 on Ints returns 0.
In Elixir at least, calling 2/0
(floating division) or div(2, 0)
(integral division) both throw an ArithmeticError.
For the sake of minimal compatibility with Elm right now it returns :nan
and :infinity
atoms.
But obviously no other function accepts them so it's more than a temporary solution
https://github.com/wende/elchemy-core/blob/e3cc1d7029cf2ed1df602f7c43f6fe98470ab3ed/elm/Elchemy/XBasics.elm#L88-L95
How about controversial opinion making division return Maybe
instead, but only when importing Elchemy?
Technically I'd think division should return a maybe unless the compiler could otherwise infer it is impossible for the denominator to be 0 (needs some measure of refined typing then). ^.^;
@OvermindDL1 Should I create a card for implementing dependent types maybe? ;)
@wende Lol, to do that you've have to re-implement Elm's typing system (or a better one...) before adding the refined typing enhancements. ;-)
I like the Maybe from dividing rather than having a specific type that denotes "NonZero". It's the same result (Maybe from divide has to be handled) with less machinery to implement.
See: https://ellie-app.com/436z6GwVva1/0
@loganmac That's a good point but it could make false assumption of type-safety, when it's not REALLY safe, just contractual (someone could make NonZero 0
and see the world burn)
The problem with making the division return Maybe is with overriding Basics, which cannot be easily done, so we end up with:
So the only solution I can see right now is:
Make Basics./
and Basics.//
throw error and make new operators to return a Maybe:
Elchemy.:/
and Elchemy.://
?
@wende I hinted at you not being able to do that by not exposing the constructor, and only the function that can return NonZero
types.
Yeah, maybe something like /?
to denote safe division that results in a Maybe? And also doesn't clash with Basics
@loganmac Oh I like the /?
!