implicit conversion over multiple levels, why does int to double automatically work?

后端 未结 2 798
南方客
南方客 2021-01-04 12:06

I was always figuring implicit conversions over multiple levels is not possible in scala (unless you define view bounds: http://docs.scala-lang.org/tutorials/FAQ/context-and

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-04 12:18

    As Gabor already commented, this is due to numeric widening. If you run with the -print option, you will see that a .toDouble is appended to the i, which then allows it to use the toA implicit. You can run scalac with the warn-numeric-widen and this will at least give you the following:

    :14: warning: implicit numeric widening
         println(i.total) //Why does this work?
                 ^
    

提交回复
热议问题