You're right, the above does the same thing. Howev...
# san-diego
b
You're right, the above does the same thing. However, they are two different approaches. The first approach is an function that takes any type
T
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:
Copy code
fun main() {
  otherExecute("blah")
}
When compiled to bytecode, it'll essentially be this in Java:
Copy code
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
K 1
👍 2
thank you color 1