Tóth István Zoltán
06/19/2024, 6:04 AMTóth István Zoltán
06/19/2024, 6:04 AM@Adaptive
fun someFun() {
text("Hello World!")
}
@Adaptive
fun text(content : String) {
// whatever text does with the content
}
Is transformed into (simplified of course):
@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.
@Adaptive
fun someFun() {
text("Hello World!")
}
Transformed into something like this:
@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?Tóth István Zoltán
06/19/2024, 9:41 AMprivate 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