What are the rules of semicolon inference?

前端 未结 3 1417
渐次进展
渐次进展 2021-02-19 00:55

Kotlin provides “semicolon inference”: syntactically, subsentences (e.g., statements, declarations etc) are separated by the pseudo-token SEMI, which stands for “

相关标签:
3条回答
  • 2021-02-19 01:36

    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:

    fun returnFun() : (x: Int) -> Unit {
      println("foo")
      return { x -> println(x) }
    }
    
    fun main(args: Array<String>) {
        println("Hello, world!")
        returnFun()
           (1 + 2)    // The returned function is not called.
    }
    

    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:

    fun voidFun() : Unit {
        println("void")
    }
    
    fun foo() : Unit {
        if (1 == 1) return 
        voidFun()  // Not called.
     }
    
    fun bar() : Unit {
        if (1 == 1)
            return 
                voidFun()  // Not called.
     }
    

    The bar function could potentially happen, if the return voidFun() wouldn't fit on one line. That said, must developers would simply write the call to the function on a separate line.

    0 讨论(0)
  • 2021-02-19 01:51

    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:

    enum class Things {
        ONE, TWO;
    
        fun isOne(): Boolean = this == ONE
    }
    

    And in this case the compiler will tell you directly if you fail to do it correctly:

    Error:(y, x) Kotlin: Expecting ';' after the last enum entry or '}' to close enum class body

    Otherwise the only other common case is when you are doing two statements on the same line, maybe for brevity sake:

    myThingMap.forEach { val (key, value) = it; println("mapped $key to $value") } 
    

    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 became init { ... } which no longer needed the semicolon because it is much clearer. These cases no longer remain in the language.

    Confidence in this feature:

    Also I have suspicions that there are some cases where this feature may not work correctly and cause problems.

    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):

    fun whatever(msg: String, optionalFun: ()->Unit = {}): () -> Unit = ...
    
    val doStuff: () -> Unit = when(x) {
        is String -> {
            { doStuff(x) }
        }
        else -> { 
            whatever("message") // absence or presence of semicolon changes behavior
            { doNothing() }
        }
    }
    

    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 suggested emit 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.

    0 讨论(0)
  • 2021-02-19 01:59

    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:

    val doStuff: () -> Unit = when(x) {
        is String -> {
            { doStuff(x) }
        }
        else -> { 
            println("This is the alternate");  // Semicolon needed here
            { doNothing() }
        }
    }
    

    Without the semicolon, Kotlin thinks the { doNothing() } statement is a second argument to println() and the compiler reports an error.

    0 讨论(0)
提交回复
热议问题