3 ms·
Now this is ugly: x.add(x.add(ONE).pow(2).subtract(x)) Especially because sometimes it is difficult to decide whether you should have x.foo(y) or y.foo(x)
by exDM69 11y ago
Now this is ugly:
x.add(x.add(ONE).pow(2).subtract(x))
Especially because sometimes it is difficult to decide whether you should have x.foo(y) or y.foo(x). However,
add(x, sub(pow(add(x, ONE), 2), x))
Is perfectly acceptable in my opinion (add some indentation if it's difficult to read). As long as you can pass parameters and return values without tricks (ie. passing pointers), this is quite nice. You should even be able to do this in Java with "import static". Or C++ with function overloading.
I'm definitely not a fan of C++ -style operator overloading, where you only have a finite amount of operators with set precedence/associativity and then they get overloaded to meanings that the symbols don't convey. On the other hand, I'm not sure what to think of Haskell either (where you can define arbitrary operators, like >>=, <+> or <$>). It's not as limited as C++, but there are some quite nasty examples that have gone overboard with operators.
Overall, it seems like operator overloading adds a lot of complexity to a language but the benefit is arguable.
- eveningcoffee 11y agoHowever I comletelly agree with this, it is very confusing API. You should even be able to do this in Java with "import static" Yep, you should define a static methods add, sub, pow etc with BigDecimal parameters and then use import static. Something along class BigDecimalMath { public static BigDecimal add(BigDecimal x1, BigDecimal x2) { return x1.add(x2); } } Also JVM JIT would very likely inline such code, so you should not really worry about an added method call.
- kilink 11y agoToo bad you can't just call functional interfaces in Java 8, or you could just do something like this: BiFunction<BigDecimal, BigDecimal, BigDecimal> add = BigDecimal::add; add(x, y); Unfortunately you have to do: add.apply(x, y); Which is kind of cumbersome.
- eveningcoffee 11y agoYes, it pretty much defeats the point. It would work if they had implemented a default method for an interface. Unfortunately they brought something completely different in the name of Default Methods, and that was in my opinion not needed.