Search code examples
scalatypesinfix-operator

Scala Infix Type Puzzle


I had understood that prefix and infix were equivalent. Why then do the following give different type answers?

3.*(5)                                        //> res50: Double = 15.0
3*5                                           //> res51: Int(15) = 15

Solution

  • I'd guess what's happening here has nothing to do with infix vs. prefix.

    It's almost certainly tokenizing 3.*(5) as 3., *, (, 5, ). The 3. is equivalent to 3.0 -- a floating point number. So, it's parsed as an infix expression: 3.0, *, 5 (with redundant parens around the 5).

    Since 3.0 is floating point, the result is floating point as well.