Scala Programming Language
  1. Scala Programming Language
  2. SI-6567

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

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: Scala 2.9.1
    • Fix Version/s: Scala 2.10.1
    • Component/s: Misc Library
    • Environment:

      IntelliJ IDEA 11.0.1 (#IU-111.167)
      Scala Plugin 0.5.275

      Description

      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

        Hide
        Jason Zaugg added a comment -

        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.

        https://github.com/scala/scala/pull/1565

        Show
        Jason Zaugg added a comment - 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 . https://github.com/scala/scala/pull/1565
        Hide
        Alex Luketa added a comment -

        Thanks Jason

        Show
        Alex Luketa added a comment - Thanks Jason
        Hide
        Jason Zaugg added a comment -
        Show
        Jason Zaugg added a comment - Merged for 2.11 https://github.com/scala/scala/commit/2c6777f
        Hide
        Adriaan Moors added a comment -

        reopening for 2.10.1-RC1 backport

        Show
        Adriaan Moors added a comment - reopening for 2.10.1-RC1 backport
        Hide
        Jason Zaugg added a comment -
        Show
        Jason Zaugg added a comment - backport: https://github.com/scala/scala/pull/1954

          People

          • Assignee:
            Jason Zaugg
            Reporter:
            Alex Luketa
          • Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development