scalaserializablecase-class

java.io.Serializable to Seq Scala


I have a case match statement that provides the following output. I'm trying to convert this into a sequence instead. Can you please help me with this?

java.io.Serializable = List(TableInfo(X,XX,List(aa@aa.com, bb@bb.com)), 
TableInfo(Y,YY,List(aa@aa.com, bb@bb.com)))

[Code]:

scala.util.Either[Exception,List[TableInfo]] = 
Right(List(TableInfo(X,XX,List(aa@aa.com, bb@bb.com)), 
TableInfo(Y,YY,List(aa@aa.com, bb@bb.com))))

result match {
  case Left(s) => s
  case Right(i) => i
}

Complete Code: https://scastie.scala-lang.org/HT8wmYtsRF6DwzEkJYeygA


Solution

  • Your code doesn't throw any exceptions right now. Based on the comments, it sounds like your intention is to extract a Right or throw whatever's on the Left. In that case, all you're missing is the throw keyword.

    result match {
      case Left(s) => throw s
      case Right(i) => i
    }
    

    throw breaks the normal flow of control, so it returns Nothing, which is the unique subtype of all types in Scala. Thus, the common types of Nothing and whatever s is, is simply the type of s.