Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - kotlin infix not working on one parameter extension function

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:

enter image description here

its not being infix and it follows the rule for infix that:

  1. Must be member functions or extension functions.
  2. They must have a single parameter.
  3. The parameter must not accept a variable number of arguments and must have no default value.

what am i doing wrong ?

UPDATE:
I ALSO tried this but its working by explicitly calling the referring class: enter image description here

since now im using the explicit object why did it fail ? ivLogo is a ImageView synthetic from kotlin.

like image 233
j2emanue Avatar asked Mar 04 '23 02:03

j2emanue


1 Answers

To make infix function work, to the left of it should be placed an actual instance of object:

val result = someView isValidColor "#FFFFFF"
like image 78
Sergey Avatar answered Apr 06 '23 00:04

Sergey