take a look at this simple extension function i have infix:
infix fun View.isValidColor(hexColor: String?): Boolean {
var isValid = true
return hexColor?.let {
try {
Color.parseColor(it)
} catch (e: Throwable) {
isValid = false
}
isValid
} ?: false
}
//notice how i have infix the extension meaning brackets are not needed, hopefully making it easier to read.
Now lets see the usage and what i have tried:
its not being infix and it follows the rule for infix that:
what am i doing wrong ?
UPDATE:
I ALSO tried this but its working by explicitly calling the referring class:
since now im using the explicit object why did it fail ? ivLogo is a ImageView synthetic from kotlin.
To make infix function work, to the left of it should be placed an actual instance of object:
val result = someView isValidColor "#FFFFFF"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With