Skip to content

Potential bug converting an Option of Java BigDecimal to an Option of Scala BigDecimal #6567

Closed

Description

@scabug

I have been experience problems converting between an Option of a Java BigDecimal to an Option of a Scala BigDecimal.

For example, the following assertion fails:

import java.math.{BigDecimal => JBigDecimal}

object BigDecimalBug extends App {
  val nullJavaBigDecimal: JBigDecimal = null
  val x: Option[BigDecimal] = Option(nullJavaBigDecimal)
  assert(x == None)
}

The variable x is not actually None but a Some of the Scala BigDecimal object. The Scala BigDecimal object is incorrectly formed with the internal data structure set to null.

I have resolved this using the following implicit conversion, but wanted to highlight this in case this is perceived as an issue with the BigDecimal conversion logic.

object BigDecimalBug extends App {
  implicit def convertJBigDecimalOption(javaBigDecimal: JBigDecimal): Option[BigDecimal] = 
    Option(javaBigDecimal) map { x => BigDecimal(x.toString) }

  val nullJavaBigDecimal: JBigDecimal = null
  val y: Option[BigDecimal] = nullJavaBigDecimal
  assert(y == None)
}

Activity

  1. scabug commented on Oct 25, 2012

    @scabug
    Author

    Imported From: https://issues.scala-lang.org/browse/SI-6567?orig=1
    Reporter: Alex Luketa (aluketa)
    Affected Versions: 2.9.1

  2. scabug commented on Nov 3, 2012

    @scabug
    Author

    @retronym said:
    It seems that applying an implicit view in the argument to Option.apply would almost always be a bug.

    I propose to add a warning under the compiler option -Xlint.

    scala/scala#1565

  3. scabug commented on Nov 3, 2012

    @scabug
    Author

    Alex Luketa (aluketa) said:
    Thanks Jason

  4. scabug commented on Nov 4, 2012

    @scabug
    Author

    @retronym said:
    Merged for 2.11 scala/scala@2c6777f

  5. scabug commented on Jan 22, 2013

    @scabug
    Author

    @adriaanm said:
    reopening for 2.10.1-RC1 backport

  6. scabug commented on Jan 23, 2013

    @scabug
    Author

    @retronym said:
    backport: scala/scala#1954

  7. added this to the 2.10.1 milestone on Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions