Alexey Romanov
Alexey Romanov

Reputation: 170919

type-case in a type synonym

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] => ...
}

?

Upvotes: 3

Views: 133

Answers (2)

lmm
lmm

Reputation: 17431

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 = ...

Upvotes: 1

Miles Sabin
Miles Sabin

Reputation: 23056

No.

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

Upvotes: 5

Related Questions