Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning a path-dependent type from a pattern match

Given a heterogeneous type:

trait Request {
  type Result
}

trait IntRequest extends Request {
  type Result = Int
}

How can I make the Scala compiler happy about return a path dependent type based on a pattern match:

def test(in: Request): in.Result = in match {
  case i: IntRequest => 1234
  case _ => sys.error(s"Unsupported request $in")
}

The error:

<console>:53: error: type mismatch;
 found   : Int(1234)
 required: in.Result
         case i: IntRequest => 1234
                               ^
like image 268
0__ Avatar asked Jul 16 '14 16:07

0__


1 Answers

The following works:

trait Request {
  type Result
}

final class IntRequest extends Request {
  type Result = Int
}

trait Service {
  def handle[Res](in: Request { type Result = Res }): Res
}

trait IntService extends Service {
  def handle[Res](in: Request { type Result = Res }): Res = in match {
    case i: IntRequest => 1234
    case _ => sys.error(s"Unsupported request $in")
  }
}

trait Test {
  def service: Service

  def test(in: Request): in.Result = service.handle[in.Result](in)
}

The compiler only eats it if one uses a final class, though?!

like image 68
0__ Avatar answered Nov 15 '22 04:11

0__