I think I found a compiler bug :eyes:
# compose-web
m
I think I found a compiler bug 👀
Code that causes this issue (yeah, the comments doesn't really make too much sense, what I was doing is a function that allows replacing text Intl MessageFormat-like "Hello World! {clickHere}" messages with a composable function in the "clickHere" section)
Copy code
@Composable
fun Test(text: String, callback: @Composable (String) -> (Unit), customComposeKey: String, callback2: @Composable () -> (Unit)) {
    val currentSection = StringBuilder()
    val sectionName = StringBuilder()
    var inSection = false

    for (char in text) {
        if (char == '}') {
            inSection = false

            val sectionNameAsString = sectionName.toString()
            if (customComposeKey == sectionNameAsString) {
                // Has a custom composable section!
                callback.invoke(currentSection.toString()) // So first, we are going to append everything before this section
                callback2.invoke() // And then we are going to call our custom composable section!
                sectionName.clear()
            } else {
                // Doesn't has a custom composable section, just append the section as is and let the formatter do its thing
                currentSection.append("{$sectionName}")
                sectionName.clear()
            }
            continue
        }
        if (char == '{') {
            inSection = true
            continue
        }

        if (inSection)
            sectionName.append(char)
        else
            currentSection.append(char)
    }

    // And now, invoke the left over section
    callback.invoke(currentSection.toString())
}
a
I don't know what exactly causes the compiler error but still there is little:) room for improvement in your code. try reading this article https://developer.android.com/jetpack/compose/mental-model
m
@Arslan Armanuly in this use case I'm not able to do this the "Compose"-way. The reason I did that function (which, again, it looks weird because I was removing code to make a reproduceable example without any dependencies) is to allow i18n using a MessageFormat-like format in my Compose application, allowing me to share i18n code and strings between the frontend and the backend. So in this case, I have a map with a key -> value map of my translations, example:
this.is.a.key = If you want to know more, {0}
, where I want to replace the
{0}
with a "click here" + href link. That's what the code does (which... well, should've worked if it didn't have that compiler error 😛) An alternative would be storing the translations directly in Kotlin files, however this doesn't let me use Crowdin for translations (unless if I did a code generation task in Gradle to convert YAML files to Kotlin files? Maybe...)
o
cc: @bashor
m
(This bug is also present in 1.0.0-alpha1-rc2, Kotlin 1.5.21)
b
@MrPowerGamerBR have you tried to run the same code on Desktop or Android versions? Or even without compose? Anyway, feel free to file an issue.
I guess there is something unexpected after compose transformations. (cc @Oleksandr Karpovich [JB] @jim)
Simplified example:
Copy code
@Composable
fun Test(text: String, callback: @Composable () -> Unit) {
    for (char in text) {
        if (char == '}') {
            if (Random.nextBoolean()) {
                callback()
            } else {
                println()
            }
            continue
        }
    }
}
even simper:
Copy code
@Composable
fun Test(text: String, callback: @Composable () -> Unit) {
    for (char in text) {
        if (char == '}') {
            callback()
            continue
        }
    }
}
m
@bashor no, I haven't tested with Compose Desktop/Android, but I can try it out later 🙂 I will also submit an issue, should it be reported in the "compose-jb" repository?
j
Yes, compose-web issues should be reported to compose-jb repository.
m
Tested in Compose Desktop: Also doesn't work 😭 Here's the issue 🙂 https://github.com/JetBrains/compose-jb/issues/984
o
then it will likely affect Compose on Android as well, as plugin is the same, cc: @Leland Richardson [G]
j
Thanks.  Tracking in upstream as https://issuetracker.google.com/issues/195502293
l
Good guy, Jim 👏🏼
j
Well, we usually ask the OP to file if there is any degree of ambiguity about details of the issue. In this case, the simplified example @bashor posted was so clear, concise, and complete - no additional information was needed so it was sensible to file myself.
l
Yup, makes sense 🙂