Search code examples
scalashapeless

How do I extract narrowed type and use it in Shapeless's coproduct?


Having this code:

object Types {
  val undefined = "undefined".narrow
  type RunnableArgument = BigDecimal :+: String :+: Boolean :+: CNil
}

I would like to have the type of the undefined constant as one of the RunnableArgument hlist elements. Is it even possible?


Solution

  • Singleton types used as HList values in scala 2.13, check the original SIP:

    https://docs.scala-lang.org/sips/42.type.html

    So, in your case, you can write:

    RunnableArgument = "undefined" :+: String :+: Boolean :+: CNil
    

    Also you can use this thing as type:

    type Param = "singleton type"
    

    This compiles in scastie:

    https://scastie.scala-lang.org/ObzN7xkKQ9egGBIv2gDuiA