Do anyone have a workaround for <https://youtrack....
# compiler
t
Do anyone have a workaround for https://youtrack.jetbrains.com/issue/KT-58886 Use case and details in the thread.
My use case:
Copy code
@Adaptive
fun someFun() {
    text("Hello World!")
}

@Adaptive
fun text(content : String) {
    // whatever text does with the content
}
Is transformed into (simplified of course):
Copy code
@Adaptive
fun someFun(content : String) {
    // empty body
}

@Adaptive
fun text(content : String) {
    // empty body
}

class AdaptiveSomeFun : AdaptiveFragment {
    fun genBuild() : AdaptiveFragment {
        return AdaptiveText() // <---------- constructor without parameters
    }
    fun genPatchExternal(fragment : AdaptiveFragment) {
        fragment.setStateVariable(1, "Hello World!) // <-------- fragment is AdaptiveText here
    }
}

class AdaptiveText : AdaptiveFragment {
    val state = arrayOfNulls<Any?>(1)
}
The problem is that I cannot add the classes in FIR because of KT-58886 . So the compiler won't be able to find the classes later when I try transform the call to the functions. I'm thinking about the workaround below. It's not pretty but I've struggled with this issue for quite a while and couldn't come up with a better solution.
Copy code
@Adaptive
fun someFun() {
    text("Hello World!")
}
Transformed into something like this:
Copy code
@Adaptive
fun someFun() : AdaptiveFragment {
    class AdaptiveSomeFun : AdaptiveFragment {
        fun genBuild() : AdaptiveFragment {
            return text() // <---------- constructor without parameters
        }
        fun genPatchExternal(fragment : AdaptiveFragment) {
            fragment.setStateVariable(1, "Hello World!) // <-------- fragment is AdaptiveText here
        }
    }

    return AdaptiveSomeFun()
}

@Adaptive
fun text() : AdaptiveFragment {
    class AdaptiveText : AdaptiveFragment {
        val state = arrayOfNulls<Any?>(1)
    }
    return AdaptiveText()
}
This approach has the drawback that there can't be two functions with the same name but different signature. I'm willing to accept that. Does anyone has a better approach? Is this idea viable or will removal of parameters and changing the retrun type cause problems?
Implemented the idea above, it works, but I had to: 1. Add a hackish patching of function parameters for incremental compilation to work 2. Add @HiddenFromObjC to my top level function to avoid iOS framework compilation crash The hackish patching is like this:
Copy code
private fun fixValueParameters(owner: IrSimpleFunction) {
    if (owner.valueParameters.size == 2
        && owner.valueParameters[0].type == pluginContext.adaptiveFragmentType
        && owner.valueParameters[1].type == irBuiltIns.intType
    ) return

    owner.valueParameters = emptyList()
    owner.addValueParameter(Strings.PARENT, pluginContext.adaptiveFragmentType)
    owner.addValueParameter(Strings.DECLARATION_INDEX, irBuiltIns.intType)
}
As the function definition read from KLib (I guess) is the original one, I have to patch it on-the-fly to make the module that uses that function compile. I realize that this is no good. I just don't see any other ways to go on without KT-58886 is fixed. If anyone made it this far, this is the project I'm working on. It has been inspired by Svelte but had grown a lot since the beginning. https://github.com/spxbhuhb/adaptive