https://kotlinlang.org logo
#compiler
Title
# compiler
l

louiscad

10/22/2019, 9:29 AM
I'm seeing override by an inline function is only a warning that can be suppressed. What happens under the hood when I do it? Tries to resolve to inline when the type is the impl of the interface?
i

Ilmir Usmanov [JB]

10/22/2019, 9:57 AM
inline function bodies are not deleted after inlining. Thus, calling method from interface will result in calling inline function.
l

louiscad

10/22/2019, 10:03 AM
It's only using the inline function for private usage, or it can go beyond? If it can go beyond, that's something I'd try for inline classes so I still benefit from using interfaces and keeping my code flexible to implementation changes without the overhead of autoboxing.
k

karelpeeters

10/22/2019, 10:46 AM
If the function is resolved statically it's inlined, if you call it trough the interface it's not.
Copy code
interface Foo {
    fun foo()
}

class Bar: Foo {
    override inline fun foo() = print("test")
}

fun main() {
    val x: Bar = Bar()
    val y: Foo = Bar()
    
    x.foo()
    y.foo()
}
x.foo()
is inlined,
y.foo()
is not.
i

Ilmir Usmanov [JB]

10/22/2019, 10:53 AM
It's only using the inline function for private usage, or it can go beyond?
Depends on what you mean by "beyond". It is just like normal function in bytecode and you can call it from Java.
l

louiscad

10/22/2019, 11:50 AM
@karelpeeters foo bar examples are so hard to read IMO.
InterfaceA
,
ClassB
,
someProperty
, etc are more readable, don't you think?
@Ilmir Usmanov [JB] I meant just like Karel's example, outside of the private scope of the class (public access). Now I got it, thanks for the info!
k

karelpeeters

10/22/2019, 1:10 PM
Yeah I guess, I still had some foo-bar code from another question open in InteliJ simple smile
6 Views