开发者

Adding spaces to the + operator in Scala gives different results?

开发者 https://www.devze.com 2023-03-12 01:41 出处:网络
Scala newbie here Trying (1).+(2) returns a Int value of 3, so far so good but 1.+(2) returns a Double value of 3.0.

Scala newbie here

Trying

(1).+(2) returns a Int value of 3, so far so good

but

1.+(2) returns a Double value of 3.0.

But if you do

1 . +(2) it returns a Int value of 3.

Note: The only difference between t开发者_C百科his and the above is the space after the "1"

Does Spaces matter in Scala? Im more curious as to how 1.+(2) returned a Double as it looks like it parsed 1. as a Double and then added "2" to it.


1.+(2) is calling the + method on the Double "1.". This is a carry-over from Java syntax, where "1." is equivalent to 1.0.

0

精彩评论

暂无评论...
验证码 换一张
取 消