1

I'm using JSR 354 a.k.a JavaMoney, and I don't understand what MonetaryAmount.plus() is supposed to do?

For all implementations of MonetaryAmount I found, it simply returns this. The documentation is not helpful to me:

Returns a {@code MonetaryAmount} whose value is <code>+this</code>, with rounding according to
the context settings.

This is a bit unfortunate when using Kotlin, which has operator overloading for the function plus which maps the operator + onto this function, where as it should map onto add in this case.

This can be fixed:

    operator fun MonetaryAmount.plus(other: MonetaryAmount) = this.add(other)

But I'm not sure onto what operator I should map the plus() function to? UnaryPlus?

Adam Millerchip
  • 20,844
  • 5
  • 51
  • 74
Ynv
  • 1,824
  • 3
  • 20
  • 29
  • 1
    Looks like unary plus, yes. It's useless by default but can be useful when overloaded for a custom type. In Kotlin it's called [`unaryPlus`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/unary-plus.html). – Thomas Sep 18 '20 at 13:07

0 Answers0