baxter
02/11/2024, 6:22 PMT
and if it's a string, you cast that type as such and print the length.
The second approach is actually not a function (although it is written as one). The inline
keyword basically tells the Kotlin compiler to replace any caller of the otherExecute()
function with the body you provided. So if you have the following snippet:
fun main() {
otherExecute("blah")
}
When compiled to bytecode, it'll essentially be this in Java:
class MainKt {
public static void main(String[] args) {
if (String.class.equals(String.class)) {
System.out.println(((String) "blah").getLength())
}
}
}
Now, the reified
keyword basically tells the compiler that T
shouldn't be treated as a generic, but as the actual class object. That allows you to do reflection or other operations on T
that you couldn't normally do with generics.
This article is a good read on helping understand reified
and `inline`: https://www.baeldung.com/kotlin/reified-functions