I thank the OP for pointing me towards a problem I had not considered yet: that semicolons (or other visible separator tokens), annoying as typing and reading them can be, are needed for compilers to parse every weird code formatting corner-case that programmers can think of. And if we do not want semicolons (like in Kotlin, Python, Ruby, Go, Swift and other languages) developers still have to sacrifice something, namely complete freedom of layout. No such thing as a free lunch.
Personally, I like uniform code/a uniform code style, and I am also convinced that uniform code style is the way of the future for programming languages (much better for maintenance, and professional developers tend to do much more maintenance than ‘greenfield coding’). Python’s PEP8, C#/Visual studio’s autostyling, Go’s automatic formatter… Kotlin too (in IntelliJ) has an auto-formatter, which transforms the original line into
println(
when {
eBin -> "b"
eHexa -> "h"
else -> ""
} + s
)
No semicolons there, no confusion about how many items one can squash into one line (because, frankly, you shouldn’t if you want to understand the code two months from now, and overall more readable than the original. Though, if I were to need to get this code past code review, I’d probably make it:
val prefix = when {
eBin -> "b"
eHexa -> "h"
else -> ""
}
println(prefix + s)
(and if I were really nitpicky I would point that eBin and eHexa are likely exclusive, so they would be modes in an enum, instead of separate booleans which can give weird results if more than one is set to ‘true’, enum class DisplayMode(val prefix: String) { DECIMAL(“”), BINARY(“b”), HEXADECIMAL(“x”) } …val mode = DisplayMode.HEXADECIMAL… println(“${mode.prefix}99”) which would make the when statement superfluous - but the issue here is the effects of whitespace, not domain-driven design or programming best practices)
So overall, while I agree with the OP that the Kotlin parsing situation is not ‘ideal’, and possibly there could be a request to JetBrains to make their parser yet more intelligent (though I am not sure they can/should do so, as that will decrease compilation speed and thereby negatively affect development, likely more than any added safety or convenience for corner whitespace formatting cases), I consider that as long as people write ‘default formatted code’, the problem is not that handicapping. Compared with the position of Python, for example, where the freedom from semicolons comes with sacrificing easy iterable operation chaining (or at least requires Python developers to insert extra ()), I think Kotlin/the JetBrains team is doing a pretty good job.