我们可以动态地为元组创建一个类型别名吗?

时间:2015-02-09 11:28:50

标签: scala scala-macros shapeless

假设我有一个元组

val myTuple: (String,Int,String,...,Boolean) = ("",0,"",..,true)

我可以写一个类型别名

type MyType = (String,Int,String,...,Boolean)
val myTuple: MyType = ("",0,"",..,true)

我可以动态编写此类别别名吗?有没有办法在这种类型别名中不显式,让编译器找到类型本身与别名关联?

例如

之类的例子
case class MyCaseClass(x: String,y: Int,z:String,...,zz:Boolean)

type MyDynamicTupleType = MyCaseClass.tupled.parameter1.type

不确定这是否可行,或者这是否非常有用,只要发现使用非常长的元组编写别名非常无聊并且只是样板(exemple here)。

打开基于宏观或无形的解决方案

1 个答案:

答案 0 :(得分:2)

将无形的几件组合在一起让我们大部分时间 - Genericcase class表示为HListTupler来代表{{1}作为一个元组:

HList

我不知道一种方法(缺少宏)以避免采取两个步骤来调用 - sealed trait GenericAsTuple[G] { type Out } object GenericAsTuple { implicit def fromGenericAndTupler[G, T <: HList, O]( implicit g: Generic[G]{type Repr = T}, t: Tupler[T] {type Out = O}) = new GenericAsTuple[G]{type Out = O} def apply[G](implicit g: GenericAsTuple[G]): GenericAsTuple[G]{ type Out = g.Out} = g } val myGat = GenericAsTuple[MyCaseClass] type MyDynamicTupleType = myGat.Out 需要是一个稳定的值才能声明一个类型别名,我们可以&#39 ; t只做myGat

相关问题