Kotlin provides “semicolon inference”: syntactically, subsentences (e.g., statements, declarations etc) are separated by the pseudo-token SEMI, which stands for “semicolon or newline”. In most cases, there’s no need for semicolons in Kotlin code.
This is what the grammar page says. This seems to imply that there is a need to specify semicolons in some cases, but it doesn't specify them, and the grammar tree below doesn't exactly make this obvious. Also I have suspicions that there are some cases where this feature may not work correctly and cause problems.
So the question is when should one insert a semicolon and what are the corner cases one needs to be aware of to avoid writing erroneous code?
You only need to specify semicolons in cases where it is ambiguous to the compiler what you are trying to do, and the absence of a semicolon would result in an obvious compiler error.
The rule is: Don't worry about this and don't use semicolons at all (other than the two cases below). The compiler will tell you when you get it wrong, guaranteed. Even if you accidentally add an extra semicolon the syntax highlighting will show you it is unnecessary with a warning of "redundant semicolon".
The two common cases for semi-colons:
An enum class that has a list of enums and also properties or functions in the enum requires a
;
after the enum list, for example:And in this case the compiler will tell you directly if you fail to do it correctly:
Otherwise the only other common case is when you are doing two statements on the same line, maybe for brevity sake:
Absence of a semicolon in this last example will give you a more mysterious error at the point where it is confused what you are doing. It is really hard to make some code that would both be valid as two statements separated by a semicolon that are also valid when the semicolon is removed and they become one.
In the past there were other cases like an initialization block of a class which was more "anonymous"
{ ... }
before Kotlin 1.0 and later becameinit { ... }
which no longer needed the semicolon because it is much clearer. These cases no longer remain in the language.Confidence in this feature:
The feature works well, there is no evidence anywhere that there are problems with this feature and years of Kotlin experience have not turned up any known cases where this feature backfires. If there is a problem with a missing
;
the compiler will report an error.Searching all of my open-source Kotlin, and our internal rather large Kotlin projects, I find no semi-colons other than the cases above -- and very very few in total. Supporting the notion of "don't use semicolons in Kotlin" as the rule.
However, it is possible that you can intentionally contrive a case where the compiler doesn't report an error because you created code that is valid and has different meaning with and without a semicolon. This would look like the following (a modified version of the answer by @Ruckus):
In this case doStuff is being assigned the result of the call to
whatever("message") { doNothing() }
which is a function of type()->Unit
; and if you add a semicolon it is being assigned the function{ doNothing() }
which is also of type()->Unit
. So the code is valid both ways. But I have not seen something like this occur naturally since everything has to line up perfectly. The feature suggestedemit
keyword or^
hat operator would have made this case impossible, and it was considered but dropped before 1.0 due to strongly oposed opinions and time constraints.Kotlin seems to mostly infer semicolons eagerly. There seem to be exceptions (as shown in Jayson Minard's enum example).
Generally, the type system will catch badly inferred semicolons, but here are some cases where the compiler fails.
If an invocation's arguments are in the next line (including the parenthesis), Kotlin will assume that arguments are simply a new parenthesised expression statement:
A more common case could be the following, where we have a return with the expression in the next line. Most of the time the type system would complain that there is no return value, but if the return type is
Unit
, then all bets are off:The
bar
function could potentially happen, if thereturn voidFun()
wouldn't fit on one line. That said, must developers would simply write the call to the function on a separate line.I addition to Jayson Minard's answer, I've run into one other weird edge case where a semicolon is needed. If you are in a statement block that returns a function without using the return statement, you need a semicolon. For example:
Without the semicolon, Kotlin thinks the
{ doNothing() }
statement is a second argument toprintln()
and the compiler reports an error.