karelpeeters
09/08/2019, 6:37 PMinline fun foo(block: (() -> Unit)?) {
block?.invoke()
}
Inline parameter [...] must not be nullable. Add 'noinline' modifier to the parameter declaration or make its type not nullable
Why? Would there be a problem compiling something like that? I can't imagine why.
Interestingly you can suppress this, @Suppress("NULLABLE_INLINE_PARAMETER")
but that does't actually inline the lambda, and the IDE doesn't suggest that.Adam Powell
09/08/2019, 6:52 PMrook
09/09/2019, 4:15 PMblock
to {}
. That’d remove the need for nullability altogetherkarelpeeters
09/09/2019, 5:33 PM{}
covers that nicely. Thanks!rook
09/09/2019, 5:46 PMnoinline
? The function that takes the lambda argument can be inlined, but the generated lambda can’t be inlined if it’s null, right?
this seems to work:```
private inline fun foo(noinline bar: (() -> Unit)?) {
bar?.invoke()
}
```karelpeeters
09/09/2019, 6:12 PMinline
entirely. Why is there a problem with inlining a nullable lamµbda in the first place?rook
09/09/2019, 7:31 PMinline
was to place the contents of a method at the call site at compile time. Doesn’t this still accomplish that? It just doesn’t also erase the argument’s signature. So net method change is +1 rather than 0 or +2. Not exactly what we’re wanting, but the purpose isn’t entirely defeated. As to why, I’ll have to defer to someone with more knowledge.karelpeeters
09/09/2019, 7:34 PMinline
is mainly to avoid that overhead, not so much the function call itself which the JVM can inline itself just fine.
InteliJ even has a warning when you're unnecessarily using inline, try eg. inline fun test() {}
.