简体   繁体   中英

Implicit class method not found

I have an object with an implicit class:

object ModelUtils {
  implicit class RichString(str: String) {
    def isNullOrEmpty(x: String): Boolean = x == null || x.trim.isEmpty
  }
}

However, when I try to use it, IntelliJ cannot find the isNullOrEmpty method:

"TestString".isNullOrEmpty

I've tried various imports to import the method to no avail. What am I missing?

The problem is probably not with the import itself, but rather with the unnecessary parameter x . If you want to call .isNullOrEmpty without any arguments, then you must use str , not x :

object ModelUtils {
  implicit class RichString(str: String) {
    def isNullOrEmpty: Boolean = str == null || str.trim.isEmpty
  }
}

import ModelUtils._

println("TestString".isNullOrEmpty)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM