1

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

1 Answer 1

8

There are two types called String in your code. One is java.lang.String, the other is the type parameter String that EitherProvidesRollback takes. I'm guessing using String as a type parameter to EitherProvidesRollback is your problem. It should only require B as its type parameter.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.