Kotlin中由于 ? 运算符而导致编译失败。

huangapple go评论111阅读模式
英文:

Compilation failed in Kotlin due to ? operator

问题

以下是代码部分的翻译:

inline fun Any?.what(ifNotNull: (Any) -> Unit, ifNull: () -> Unit) {
    if (this == null) {
        ifNull()
    } else {
        ifNotNull(this)
    }
}

@OptIn(ExperimentalContracts::class)
@JvmSynthetic
inline fun <T> T?.whatIfNull(
    whatIfNot: (T) -> Unit,
    whatIf: () -> Unit,
) {
    contract {
        callsInPlace(whatIf, InvocationKind.AT_MOST_ONCE)
        callsInPlace(whatIfNot, InvocationKind.AT_MOST_ONCE)
    }
    if (this != null) {
        whatIfNot(this)
    } else {
        whatIf()
    }
}

fun main() {
    null?.what(
        ifNull = {
            println("It Works!")
        },
        ifNotNull = {
            println("It not working!!")
        },
    )
    null?.whatIfNull(
        whatIf = {
            println("It Works!")
        },
        whatIfNot = {
            println("It not working!!")
        },
    )
}

这是代码部分的翻译,不包括问题的其他内容。

英文:

I'm trying to understand the following scenario.

inline fun Any?.what(ifNotNull: (Any) -&gt; Unit, ifNull: () -&gt; Unit) {
    if (this == null) {
        ifNull()
    } else {
        ifNotNull(this)
    }
}

@OptIn(ExperimentalContracts::class)
@JvmSynthetic
inline fun &lt;T&gt; T?.whatIfNull(
    whatIfNot: (T) -&gt; Unit,
    whatIf: () -&gt; Unit,
) {
    contract {
        callsInPlace(whatIf, InvocationKind.AT_MOST_ONCE)
        callsInPlace(whatIfNot, InvocationKind.AT_MOST_ONCE)
    }
    if (this != null) {
        whatIfNot(this)
    } else {
        whatIf()
    }
}

fun main() {
    null?.what(
        ifNull = {
            println(&quot;It Works!&quot;)
        },
        ifNotNull = {
            println(&quot;It not working!!&quot;)
        },
    )
    null?.whatIfNull(
        whatIf = {
            println(&quot;It Works!&quot;)
        },
        whatIfNot = {
            println(&quot;It not working!!&quot;)
        },
    )
}

I got something like this for main when I compiled this code.

   public static final void main() {   }

When I remove the ? operator after null, like null.whatIfNull( will get

public static final void main() {
      Object $this$whatIfNull$iv = null;
      int $i$f$whatIfNull = false;
      int var2 = false;
      String var3 = &quot;It Works!&quot;;
      System.out.println(var3);
   }

Im trying to understand this too. seems like it only compiled after removing the ?.

What could be the reason for this issue? Are there any specific blogs or articles? Please post as a comment, or kindly explain me. Thank you.!

Kotlin中由于 ? 运算符而导致编译失败。

答案1

得分: 1

代码部分不翻译,以下是翻译好的部分:

The thing is when you write null?.&lt;anythinghere&gt; it already knows that &lt;anythinghere&gt; is unreachable so it just leaves it out. You might think that null? is a value of Any? but it is not. So it does not match. Consider this code:

当你写null?.&lt;anythinghere&gt;时,它已经知道&lt;anythinghere&gt;是不可到达的,所以它会忽略它。你可能会认为null?Any?的一个值,但事实并非如此。所以它不匹配。考虑以下代码:

Output will be:

输出将会是:

null
Test

When writing null? you have an example of the first case, meaning it won't ever execute whatever is after it.

当写入null?时,你有一个第一种情况的示例,意味着它永远不会执行其后的任何内容。

英文:

The thing is when you write null?.&lt;anythinghere&gt; it already knows that &lt;anythinghere&gt; is unreachable so it just leaves it out. You might think that null? is a value of Any? but it is not. So it does not match. Consider this code:

fun main() {
    val a = null
    println(a?.test())
    println(a.test())
}

fun Any?.test() : String {
    return &quot;Test&quot;
}

Output will be:

null
Test

When writing null? you have an example of the first case, meaning it won't ever execute whatever is after it.

huangapple
  • 本文由 发表于 2023年3月7日 16:24:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659509.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定