Hi, hello everyone! So, I'm following <this book> ...
# android
b
Hi, hello everyone! So, I'm following this book from 2023 on making apps in jetpack compose, and, taking the code from the book's official github repo, the following code works:
Copy code
fun Modifier.drawYellowCross() = then(
    object : DrawModifier {
        override fun ContentDrawScope.draw() {
            drawLine(
                color = Color.Yellow,
                start = Offset(0F, 0F),
                end = Offset(size.width - 1, size.height - 1),
                strokeWidth = 10F
            )
            drawLine(
                color = Color.Yellow,
                start = Offset(0F, size.height - 1),
                end = Offset(size.width - 1, 0F),
                strokeWidth = 10F
            )
            drawContent()
        }
    }
)
However, if I try creating a new Compose project with the most recent version of Android Studio 2025, pasting that piece of code into the default "Empty Views App" project template, the code gives the following compilation error:
Modifier factory functions must use the receiver Modifier instance
Now, personally, what I understand from this error is that my
drawYellowCross()
function should be an extension function of the Modifier class... which it is. Anyway, to sum up, here are my questions: 1. How should I understand this error? 2. What changed in the versions from 2023 up 'till now? 3. How do I migrate / update my code properly? Thanks in anticipation to anyone who wishes to chime in and help.
b
Thanks, that helped! I'm proud to show off the code that I wrote to toy around and see if I understand the new syntax:
Copy code
fun Modifier.drawColoredHexagon(modifier: Modifier = Modifier, radius: Float = 40f, color: Color = Color.Yellow): Modifier =
    this then object : DrawModifier {
        override fun ContentDrawScope.draw() {
            val points = mutableVectorOf<Pair<Float, Float>>()
            var angle = 0f
            val fullCircle: Float = 2*PI.toFloat()
            val deltaAngle: Float = fullCircle/6
            while (angle <= fullCircle) {
                points.add(Pair(this.size.width/2 + radius * sin(angle), this.size.height/2 + radius * cos(angle)))
                angle += deltaAngle
            }
            var pastPoint = points[0]
            for (i in 1..6) {
                val currentPoint = points[i]
                drawLine(
                    color = color,
                    start = Offset(pastPoint.first, pastPoint.second),
                    end = Offset(currentPoint.first, currentPoint.second),
                    strokeWidth = 10f
                )
                pastPoint = currentPoint
            }
            drawContent()
        }
    }
Apologies btw for not having found the docs by myself 😅😅.