user2836797
user2836797

Reputation:

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?

Upvotes: 0

Views: 208

Answers (1)

Andrey Tyukin
Andrey Tyukin

Reputation: 44918

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)

Upvotes: 5

Related Questions