arekolek
10/12/2018, 2:16 PMprintln(4-2018.toString().length)
print in your opinion?ribesg
10/12/2018, 2:18 PM0
David W
10/12/2018, 2:18 PM0
marstran
10/12/2018, 2:19 PMmarstran
10/12/2018, 2:19 PMDavid W
10/12/2018, 2:20 PM0
ribesg
10/12/2018, 2:20 PMJakub Gwóźdź
10/12/2018, 2:21 PMputs 2018.to_s.length; # 4
puts -2018.to_s.length; # 5
puts 4-2018.to_s.length; # 0
x = 2018;
puts -x.to_s.length; # -4
I'd like to check other languages as well, but I don't know which ones allow to call methods on integer literals (java does not)Jakub Gwóźdź
10/12/2018, 2:26 PMribesg
10/12/2018, 2:27 PMelizarov
10/12/2018, 2:27 PMJakub Gwóźdź
10/12/2018, 2:27 PMCasey Brooks
10/12/2018, 2:29 PM-2018
. How should it parse it? Is the minus sign supposed to be part of the literal, or an operation on the lone number? There is no good way for the parser to make that choice and resolve the ambiguity, other than eliminating the possibility of using it as part of the literal, since it needs to recognize similar things as an operator in other contextselizarov
10/12/2018, 2:33 PM-
operator becomes this weird exception. Compare:
4-2018.toString().length
4 - 2018.toString().length
- 2018.toString().length
-2018.toString().length
In Kotlin all of that is consistent in behavior. In Java…. it is not an issue, since you cannot do 4.toString()
and you cannot have extensions on Int
in Java either. So it is logical for Java to do it the way Java does.