English 中文(简体)
如果半殖民地在lin子是选择性的,那么 k子的编纂者如何理解发言结束?
原标题:How kotlin compiler understands the end of statement if semicolon is optional in kotlin?

在java,我们利用半殖民地,根据半殖民者的理解,发言结束。

中文不适用。 用途 JRV 编辑们如何理解这一点,是在<条码>Kotlin上没有半分辨率的结束发言?

问题回答

here

In Kotlin, semicolons are optional, and therefore line breaks are significant.

this essentially means kotlinc can identify the end of a statment in same manner as javac does, only difference is the character that marks the end of statement, where javac looks for ;, kotlinc would look for a (line break).

Please note that use of semicolon is required when declaring an enum class in kotlin.

for example following code will not compile if you omit the semicolon

enum class Fruit(val price: Double) {
    MANGO(100.0);

    fun printPrice(){
        print(price)
    }
} 

<> 补充答复>

Kotlin lexer parser在通过TruncedSemantic WhiteA

This line break recognition is done without having to insert (or replace them with semicolons). Check the discussion here

Semantic WhitespaceAwarePBsiuiperImplp/<> https://github.com/JetBrains/kotlin/blob 2. 如何开展工作的方法:

@Override
public boolean newlineBeforeCurrentToken() {
    if (!newlinesEnabled.peek()) return false;

    if (eof()) return true;

    // TODO: maybe, memoize this somehow?
    for (int i = 1; i <= getCurrentOffset(); i++) {
        IElementType previousToken = rawLookup(-i);

        if (previousToken == KtTokens.BLOCK_COMMENT
                || previousToken == KtTokens.DOC_COMMENT
                || previousToken == KtTokens.EOL_COMMENT
                || previousToken == SHEBANG_COMMENT) {
            continue;
        }

        if (previousToken != TokenType.WHITE_SPACE) {
            break;
        }

        int previousTokenStart = rawTokenTypeStart(-i);
        int previousTokenEnd = rawTokenTypeStart(-i + 1);

        assert previousTokenStart >= 0;
        assert previousTokenEnd < getOriginalText().length();

        for (int j = previousTokenStart; j < previousTokenEnd; j++) {
            if (getOriginalText().charAt(j) ==  
 ) {
                return true;
            }
        }
    }

    return false;
}

每当需要检查象征性的:

private boolean tokenMatches(IElementType token, IElementType expectation) {
    if (token == expectation) return true;
    if (expectation == EOL_OR_SEMICOLON) {
        if (eof()) return true;
        if (token == SEMICOLON) return true;
        if (myBuilder.newlineBeforeCurrentToken()) return true;
    }
    return false;
}

因此,newlineBeforeCurrent Token() parses nature by nature and compars nature and compars it to a linebreak,并最终表示其发言内容完整。

Semicolons add no human-valuable meaning to the code. Life’s too short to bother with them. This is only one (admittedly minor) of many examples of non-essential complexity in Java that is removed by more modern JVM languages.

In Kotlin, Semicolons are optional, but line breaks are significant.

Kotlin汇编者通常承认发言结束,即使没有明确的<代码>;,例如通过细线休息。

<>Important: 如果您想用一条单行发言,则与<条码>分开。

例:

    val a = 42 ; println(a)




相关问题
Exposed runs query on all HikariCP open connections

I m running a Ktor server with a PostgreSQL database and just set up the JetBrains Exposed ORM framework with HikariCP for connection pooling (per the recommendation in the Ktor documentation). My ...

SQLite Kotlin issue - no database

I am trying to create boardgamegeek APIs based app in AndroidStudio, however from unknown reason I m getting my database created with no columns. Here s Logical log: (1) no such table: games in "...

Flutter App cannot be installed on Android TV

I m building a Flutter app that should support Android TV and Mobile devices. Despite Google Play shows that it is supported, I cannot install app on my MiBox device. While trying to install it, both ...