Hit a really strange issue today while trying to add an implicit method to Either.
implicit class EitherProvidesRollback[String,B](e: Either[String,B]) {
def rollback(
ss: Option[Session], overrideMsg: Option[String]): Either[String,B] = {
e.fold(
msg=> {
ss.map(_.rollback)
// found String required java.lang.String, FTW
// Left(i18n(overrideMsg.getOrElse(msg)))
// behold, the horrible hack
Left(i18n(overrideMsg.getOrElse(msg).toString).asInstanceOf[String])
},
Right(_)
)
}
}
the i18n method takes a String, which AFAICT is exactly what it's getting. The workaround, as per this thread, is to T <: String
in the implicit class' type signature. It appears Predef might be at play here.
Is there a way to get this working without horrendous runtime casting while at the same time preserving the type signature as exactly String?
Thanks
There are two types called
String
in your code. One isjava.lang.String
, the other is the type parameterString
thatEitherProvidesRollback
takes. I'm guessing usingString
as a type parameter toEitherProvidesRollback
is your problem. It should only requireB
as its type parameter.