I have some cases where I’d like methods to return either type A or some type B (A and B are some classes, like String and Int)?

You can use an Either type and pattern matching

  sealed trait Either[A,B]
  case class Left[A,B](x:A) extends Either[A,B]
  case class Right[A,B](x:B) extends Either[A,B]

Or you can use “dynamic typing”

  def foo(): Any   // should be: either A or  B
 
  foo() match {
    case a: A => ...
    case b: B => ...
  }
 
faqs/types.txt · Last modified: 2010/02/11 09:10
 
Recent changes RSS feed Valid XHTML 1.0 Driven by DokuWiki