Нет, не будет. Оператор плюс на самом деле является функцией, и он не объявлен в классе Number, но в конкретных классах, расширяющих Number
. Вы можете видеть это в ошибках компилятора:
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
@InlineOnly public inline operator fun BigDecimal.plus(other: BigDecimal): BigDecimal defined in kotlin
@InlineOnly public inline operator fun BigInteger.plus(other: BigInteger): BigInteger defined in kotlin
public operator fun <T> Array<???>.plus(elements: Array<out ???>): Array<???> defined in kotlin.collections
public operator fun <T> Array<???>.plus(elements: Collection<???>): Array<???> defined in kotlin.collections
public operator fun <T> Array<Number>.plus(element: Number): Array<Number> defined in kotlin.collections
public operator fun BooleanArray.plus(element: Boolean): BooleanArray defined in kotlin.collections
public operator fun BooleanArray.plus(elements: BooleanArray): BooleanArray defined in kotlin.collections
public operator fun BooleanArray.plus(elements: Collection<Boolean>): BooleanArray defined in kotlin.collections
public operator fun ByteArray.plus(element: Byte): ByteArray defined in kotlin.collections
public operator fun ByteArray.plus(elements: ByteArray): ByteArray defined in kotlin.collections
...
Но этот код будет работать:
// Don't try this at home !!!
fun sum(x: Number, y: Number) = x.toDouble() + y.toDouble()
Ну, он по крайней мере скомпилируется. Для двух BigDecimal вы скорее не получите ожидаемый результат.