未找到隐式 class 方法
Implicit class method not found
我有一个隐含的对象 class:
object ModelUtils {
implicit class RichString(str: String) {
def isNullOrEmpty(x: String): Boolean = x == null || x.trim.isEmpty
}
}
但是,当我尝试使用它时,IntelliJ 找不到 isNullOrEmpty 方法:
"TestString".isNullOrEmpty
我试过各种导入导入方法都无济于事。我错过了什么?
问题可能不在于导入本身,而在于不必要的参数 x
。如果你想调用 .isNullOrEmpty
而不带任何参数,那么你必须使用 str
,而不是 x
:
object ModelUtils {
implicit class RichString(str: String) {
def isNullOrEmpty: Boolean = str == null || str.trim.isEmpty
}
}
import ModelUtils._
println("TestString".isNullOrEmpty)
我有一个隐含的对象 class:
object ModelUtils {
implicit class RichString(str: String) {
def isNullOrEmpty(x: String): Boolean = x == null || x.trim.isEmpty
}
}
但是,当我尝试使用它时,IntelliJ 找不到 isNullOrEmpty 方法:
"TestString".isNullOrEmpty
我试过各种导入导入方法都无济于事。我错过了什么?
问题可能不在于导入本身,而在于不必要的参数 x
。如果你想调用 .isNullOrEmpty
而不带任何参数,那么你必须使用 str
,而不是 x
:
object ModelUtils {
implicit class RichString(str: String) {
def isNullOrEmpty: Boolean = str == null || str.trim.isEmpty
}
}
import ModelUtils._
println("TestString".isNullOrEmpty)