Goal: support run-time access to types passed to functions, as if they were reified (currently limited to inline functions only).

Sorry, I didn’t find any more information about reified. There are two documents reified- Type-Parameters and Reference/Inline-functions for your reference.

100% Interoperable with Java™ and Android™ You’ll find out after reading this article.

What is reified?

Also, this word is reified. You can think of it as embodying something. So what does it embody? Let’s see how it works first:

    inline fun <reified T> membersOf(a) = T::class.members

    fun main(s: Array<String>) {
        println(membersOf<StringBuilder>().joinToString("\n"))}Copy the code

This method means that it externalizes a type parameter of T, appends a method of membersOf, and returns members of the class T. Inline function keyword, more details in the next section.

The use of the above is also simple:

For more details, refer to the documents reified type-parameters and Reference/Inline-functions.

I love reified

I fell in love with Reified when I was working on discovery pages. Because the data structure returned by the server is similar to THE HTML structure, the hierarchy is relatively deep, and there are various types, so some concrete classes have to be derived. But here’s the problem: I need to decide whether to display the Header. This is when reified is used.

For example, declare an inline function like this:

   inline fun <reified T : BaseModel<*>> BaseModel< * >.headerEnable(a): Boolean {
        val headerData = this.dataHeader<T>()
        returnheaderData ! =null&& headerData.metadata ! =null
    }Copy the code

When called:

    val headerEnable = lastItem.headerEnable<FrameListModel>()Copy the code

Is it super simple and convenient?

I do not love reified

Someone told me that Kotlin supports Java 100%, so I’m telling you, you’re naive. No, reified for example is not supported. So be careful when working on a project that is an SDK.

There’s no way to call Kotlin inline functions from Java because they must be transformed and inlined at the call sites (in your case, T should be substituted with the actual type at each call site, but there’s much more compiler logic for inline functions than just this), and the Java compiler is, expectedly, completely unaware of that. how-can-i-call-kotlin-methods-with-reified-generics-from-java

Write in the last

Reified means to be embodied. As one of Kotlin’s method generics keywords, it means that you can access the JVM class objects specified by generics in the method body. This method must be declared inline to be valid. As for the specific internal how to achieve, I leave it to you to study, of course, I will explain in the later article, today will not extend.

If you are willing to listen to me talk about technology, you can follow my personal public account SKMacTalk:





SKMacTalk.png