What is the basic design idea behind the Scala for-loop implicit box/unboxing of numerical types?

Posted by IODEV on Stack Overflow See other posts from Stack Overflow or by IODEV
Published on 2012-03-27T11:35:43Z Indexed on 2012/03/27 17:29 UTC
Read the original article Hit count: 274

Filed under:
|

I'm trying to understand the behavior of Scala for-loop implicit box/unboxing of "numerical" types. Why does the two first fail but not the rest?

1) Fails:

scala> for (i:Long <- 0 to 10000000L) {}

      <console>:19: error: type mismatch;<br>
      found   : Long(10000000L)
      required: Int
              for (i:Long <- 0 to 10000000L) {}
                                  ^

2> Fails:

scala> for (i <- 0 to 10000000L) {}

      <console>:19: error: type mismatch;
      found   : Long(10000000L)
      required: Int
          for (i <- 0 to 10000000L) {}
                         ^

3) Works:

scala> for (i:Long <- 0L to 10000000L) {}

4) Works:

scala> for (i <- 0L to 10000000L) {}

© Stack Overflow or respective owner

Related posts about numerical

Related posts about autoboxing