隐类方法未找到

问题描述 投票:0回答:1

我有一个隐含的类的对象:

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

然而,当我尝试使用它,可以的IntelliJ找不到isNullOrEmpty方法:

"TestString".isNullOrEmpty

我已经试过各种进口导入的方法都无济于事。我在想什么?

scala implicit
1个回答
5
投票

这个问题可能是无法与进口本身,而是与不必要的参数x。如果你想打电话.isNullOrEmpty不带任何参数,则必须使用str,不x

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

import ModelUtils._

println("TestString".isNullOrEmpty)
© www.soinside.com 2019 - 2024. All rights reserved.