With typical classes, you can create an implicit class to add new methods to other class instances, just like this:
implicit class IntWrapper(i: Int) {
def otherMethod(s: String) = println(s"i: ${i} with ${s}")
}
now you can do
3.otherMethod("something")
My questions are
is it possible to add a method to the to the(Answered here)Int
companion object
?Imagine there's a Java
enum
defined out of your project, and you are using it in your Scala code. Javaenums
have the.valueOf(s: String)
method that gets an enumeration value from aString
, but that throws anIllegalArgumentException
. I would like to add a method.safeValueOf(s: String)
that would return anOption
.Is it possible to do add that method to a Java
enum
in the same way we can do in the first example?