Sign in

Rafael Tonholo

@rafael.tonholo.dev
940 followers 613 following 108 posts

Android Developer 💜 Kotlin lover 📱 Jetpack Compose addicted 💡 Always trying to learn new things 🇧🇷🇨🇦

PostsRepliesMedia
Rafael Tonholo @rafael.tonholo.dev · 12/12/2024
My new favourite Kotlin feature is "when guards"! Since Kotlin 2.1.0, you can now add extra conditions to the when expression, which helps perform multiple checks while keeping type exhaustiveness. I'm using just the basics and need to read more about it, but this feels good! #KotlinMultiplatform
The image presents a code that exemplifies the usage of the when guards on Kotlin 2.1.0. Here is the code:

private fun Selector.calculateSpecificity(): CssSpecificity {
    var a = 0
    var b = 0
    var c = 0
    when (this) {
        is Selector.Id -> a = 1

        is Selector.Attribute,
        is Selector.Class -> b = 1

        is Selector.Type if name != "*" -> c = 1

        is Selector.PseudoClass -> {
            val specificity = calculateSpecificity()
            a = specificity.a
            b = specificity.b
            c = specificity.c
        }

        is Selector.PseudoElement -> c = 1

        else -> Unit
    }
    return CssSpecificity(a, b, c)
}
2193
Rafael Tonholo @rafael.tonholo.dev · 17/09/2024
Espero q continue crescendo! Aqui é bem 😊
130