Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion library/src/scala/math/BigDecimal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import java.math.{
RoundingMode => JRM,
}
import scala.collection.immutable.NumericRange
import scala.runtime.ScalaRunTime.mapNull

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

// For the following function, both the parameter and the return type are non-nullable.
// However, if a null reference is passed explicitly, this method will still return null.
// We intentionally keep this signature to discourage passing nulls implicitly while
// preserving the previous behavior for backward compatibility.

/** Implicit conversion from `java.math.BigDecimal` to `scala.BigDecimal`. */
implicit def javaBigDecimal2bigDecimal(x: BigDec | Null): BigDecimal | Null = if (x == null) null else apply(x)
implicit def javaBigDecimal2bigDecimal(x: BigDec): BigDecimal = mapNull(x, apply(x))
}

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/explicit-nulls/pos/i24599.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import java.math.{BigDecimal => JBigDecimal}
import java.math.{BigInteger => JBigInteger}

opaque type CurrencyValue = BigDecimal

extension (value: CurrencyValue)
def negate: CurrencyValue = value.bigDecimal.negate().nn

def testBigDecimalConversion(jbd: JBigDecimal): BigDecimal = jbd
def testBigIntConversion(jbi: JBigInteger): BigInt = jbi
Loading