Re: [scala-language] Inconsistent behaviour of bitwise operators

35 views
Skip to first unread message

Som Snytt

unread,
Mar 29, 2014, 10:09:29 AM3/29/14
to scala-l...@googlegroups.com
The spec says the result should be the type of the receiver, and the op is overloaded for Int and Long, but constant folding doesn't honor that when there is a Long involved.

1.toInt is not folded, so that's why r6 stays an Int.

I notice the scaladoc for >> says left instead of right.  The boy sitting next to me on the bus to a field trip sang endlessly, "This is my right hand way up high, this is my left hand touch the sky."

"A bit stupid" is a good pun for this case.



On Fri, Mar 28, 2014 at 5:18 PM, Michał Pociecha <michal....@gmail.com> wrote:
Hello,
I'm curious why bitwise operators for certain numeric types work in such a strange way.

scala> :paste
// Entering paste mode (ctrl-D to finish)

val int: Int = 1
val long = 2L

val r = int << long
val r2 = int << 2L
val r3 = 1 << long
val r4 = 1 << 2L
val r5 = 1.toInt << long // right, a bit stupid but just to check it out
val r6 = 1.toInt << 2L

// Exiting paste mode, now interpreting.

int: Int = 1
long: Long = 2
r: Int = 4
r2: Int = 4
r3: Int = 4
r4: Long = 4
r5: Int = 4
r6: Int = 4

r4: The compiler changes this 1: Int to Long but why? Is it a bug?

Thanks,
Michal

--
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-languag...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages