type-case in a type synonym

149 Views Asked by At

Shapeless allows type-specific cases in polymorphic functions. Is there some way to achieve the same with a type member, and get the moral equivalent of

type Foo[T] = T match {
  case Int => ...
  case List[A] => ...
}

?

2

There are 2 best solutions below

0
On BEST ANSWER

No.

No, there really isn't. Really. No. ... is that 30 characters yet?

1
On

Depending on your use case you can emulate a type-level function by providing implicits for the cases (this is how a lot of shapeless works):

sealed trait Foo[T] {
  type Out
}
object Foo {
  implicit object IntFoo extends Foo[Int] {
    type Out = String
  }
  implicit def list[A] = new Foo[List[A]] {
    ...
  }
}

def myPolymorphicFunction[T](t: T)(implicit foo: Foo[T]): foo.Out = ...