Skip to content

Commit 5684187

Browse files
Backport "Fix #24599: Remove | Null from implicit BigDecimal conversion similar to BigInt" to 3.8.0 (#24613)
Backports #24600 to the 3.8.0-RC3. PR submitted by the release tooling. [skip ci]
2 parents 34717d1 + 9fc0ece commit 5684187

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

library/src/scala/math/BigDecimal.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import java.math.{
2222
RoundingMode => JRM,
2323
}
2424
import scala.collection.immutable.NumericRange
25+
import scala.runtime.ScalaRunTime.mapNull
2526

2627
object BigDecimal {
2728
private final val maximumHashScale = 4934 // Quit maintaining hash identity with BigInt beyond this scale
@@ -304,8 +305,13 @@ object BigDecimal {
304305
/** Implicit conversion from `Double` to `BigDecimal`. */
305306
implicit def double2bigDecimal(d: Double): BigDecimal = decimal(d)
306307

308+
// For the following function, both the parameter and the return type are non-nullable.
309+
// However, if a null reference is passed explicitly, this method will still return null.
310+
// We intentionally keep this signature to discourage passing nulls implicitly while
311+
// preserving the previous behavior for backward compatibility.
312+
307313
/** Implicit conversion from `java.math.BigDecimal` to `scala.BigDecimal`. */
308-
implicit def javaBigDecimal2bigDecimal(x: BigDec | Null): BigDecimal | Null = if (x == null) null else apply(x)
314+
implicit def javaBigDecimal2bigDecimal(x: BigDec): BigDecimal = mapNull(x, apply(x))
309315
}
310316

311317
/**
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import java.math.{BigDecimal => JBigDecimal}
2+
import java.math.{BigInteger => JBigInteger}
3+
4+
opaque type CurrencyValue = BigDecimal
5+
6+
extension (value: CurrencyValue)
7+
def negate: CurrencyValue = value.bigDecimal.negate().nn
8+
9+
def testBigDecimalConversion(jbd: JBigDecimal): BigDecimal = jbd
10+
def testBigIntConversion(jbi: JBigInteger): BigInt = jbi

0 commit comments

Comments
 (0)