Compiler can not parse integer with leading zero


#1

No lines containing an integer literal with a leading zero will compile or execute in the REPL:

Linux> ./kotlinc/bin/kotlinc
Welcome to Kotlin version 1.2.40 (JRE 1.8.0_91-8u91-b14-1~bpo8+1-b14)
Type :help for help, :quit for quit

1
1

01
… ;
error: unexpected tokens (use ‘;’ to separate expressions on the same line)
01
^
error: expecting an element
;
^


#2

I think a leading zero is not allowed because a lot of developers may think that the number is in octal then, and octal numbers are not supported: https://kotlinlang.org/docs/reference/basic-types.html#literal-constants

So a leading zero is used to indicate that you want to have another numeral system, and you have to specify which system you want: b for binary and x for hexadecimal.


#3

Yes. I was just looking at the kotlin-spec (altough it is no longer up to date). It states

It is an error if a decimal literal other than 0 starts with digit 0. [Example: Literals 00, 007 are errors. End Example] [Rationale: Some programming languages use a leading zero to denote octal literals. Kotlin does not support octal literals, so without this rule there would be a possibility of difficult-to-catch mistakes when a user enters a literal starting with a leading zero and intends it to be interpreted as an octal literal, but the compiler simply drops leading zeros and interprets it as a decimal literal with a different numeric value than intended. End Rationale]


#4

var x = -01 also fails, and surely is harder to justify as an octal halucination deserving a parse failure.


#5

If you look at the specs, this is explained as well. There are no negative number literals in kotlin. -1 is handled as a unary minus operator and a positive number one. Therefore the same rules with leading zeros apply. Also how would you write a negative number using octal if not as -01?


#6

Well, the answer is simple enough – when you enter octal digits on the control panel of your 8080 computer (typically to jump to the tape reader subroutine), there is no “minus” togle; the concept is missing in both reality and philosophy.

What I am trying to say is that the response to:

opt> echo “var x = 01;” > test.kts
opt> time ./kotlinc/bin/kotlinc test.kts

should not be:

test.kts:1:10: error: property getter or setter expected
var x = 01;
^
test.kts:1:10: error: unsupported [literal prefixes and suffixes]
var x = 01;
^
opt> time ./kotlinc/bin/kotlinc test.kts test.kts:1:10: error: property getter or setter expected
var x = 01;
^
test.kts:1:10: error: unsupported [literal prefixes and suffixes]
var x = 01;
^

real 0m56.445s
user 0m28.050s
sys 0m9.530s

is insufficiant to octal programmers and everyone else.