英文:
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) -> 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 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 = "It Works!";
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.!
答案1
得分: 1
代码部分不翻译,以下是翻译好的部分:
The thing is when you write null?.<anythinghere>
it already knows that <anythinghere>
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?.<anythinghere>
时,它已经知道<anythinghere>
是不可到达的,所以它会忽略它。你可能会认为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?.<anythinghere>
it already knows that <anythinghere>
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 "Test"
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论