Infertile HList Types Extending the Common Trait

I have some types based on formless HLists:

type t1 = Int :: String :: Int :: HNil
type t2 = String :: String :: Int :: HNil

I would like to define a sealed attribute ST, which is a super-type for all of them, so if I have the following function:

def fun(x:ST) = …

The following conditions are true:

fun(5 :: "foo" :: 3 :: HNil)       // It is a t1
fun("foo" :: "bar" :: 42 :: HNil)  // It is a t2

but the following does not compile:

fun(5 :: 3 :: HNil)

How to determine t1and t2how subtypes ST?

UPDATE

I think Coproducts might be the solution

type ST = t1 :+: t2 :+: CNil

fun(Coproduct[ST](5 :: "foo" :: 3 :: HNil)) // compiles
fun(Coproduct[ST](5 :: 3 :: HNil))          // does not compile
+4
source share
1 answer

It is not possible to "make" a type in a subtype of something using an alias that is just a new name. Although you could use coproduct, it would be more natural to create a new class type:

import shapeless._

type t1 = Int :: String :: Int :: HNil
type t2 = String :: String :: Int :: HNil

trait Funnable[A]

implicit object t1Funnable extends Funnable[t1]
implicit object t2Funnable extends Funnable[t2]

def fun[A: Funnable](x: A) = x

, , , , , .

+3

All Articles