I just discovered this is valid Kotlin

I just discovered this is valid Kotlin

And Java too!


val x = 1 - - - - - - - - - - - 2 val y = 3 + + + + + + + + + + + 4 val z = 5 + - + - + - + - + - + 6

I actually figured this out while working on my own lexer – it’s basically so that numeric literals with a leading + or – can be disambiguated from arithmetic operations, which requires information more easily acquired later during parsing. Otherwise, this would be difficult to lex correctly:


val w = 3+4

Also fun to note:


val x = 1-----------2 // Lexer detects decrement ops, fails val y = 3+++++++++++4 // Lexer detects increment ops, fails val z = 5+-+-+-+-+-+6 // This works!

So now you can confuse coworkers and increase job security with more obscure and difficult parses. Enjoy!

EDIT: I just discovered this as well:


val x = 3 - +(2 + 4) // Totally valid

Kotlin I believe makes ‘unaryPlus’ an explicit operation, but Java was a surprise to me. Even putting non-constant expressions in the above will work just fine.

submitted by /u/frizzil
[link] [comments]