在scala中,如何为不同长度的参数列表生成样板代码?

时间:2015-05-11 11:43:42

标签: scala

例如下面的代码,推荐的生成方式是什么?

final class Projection2[T1,T2](
  override val _1: SimpleColumn[T1],
  override val _2: SimpleColumn[T2])
extends Tuple2(_1,_2) with Projection[(T1,T2)] {
  def ~[U](c: SimpleColumn[U]) = new Projection3(_1,_2,c)
  def getResult(rs: PositionedResult) =
    (_1.getResult(rs),
     _2.getResult(rs))
}

final class Projection3[T1,T2,T3](
  override val _1: SimpleColumn[T1],
  override val _2: SimpleColumn[T2],
  override val _3: SimpleColumn[T3])
extends Tuple3(_1,_2,_3) with Projection[(T1,T2,T3)] {
  def ~[U](c: SimpleColumn[U]) = new Projection4(_1,_2,_3,c)
  def getResult(rs: PositionedResult) =
    (_1.getResult(rs),
     _2.getResult(rs),
     _3.getResult(rs))
}

final class Projection4[T1,T2,T3,T4](
  override val _1: SimpleColumn[T1],
  override val _2: SimpleColumn[T2],
  override val _3: SimpleColumn[T3],
  override val _4: SimpleColumn[T4])
extends Tuple4(_1,_2,_3,_4) with Projection[(T1,T2,T3,T4)] {
  def ~[U](c: SimpleColumn[U]) = new Projection5(_1,_2,_3,_4,c)
  def getResult(rs: PositionedResult) =
    (_1.getResult(rs),
     _2.getResult(rs),
     _3.getResult(rs),
     _4.getResult(rs))
}

final class Projection5[T1,T2,T3,T4,T5](
  override val _1: SimpleColumn[T1],
  override val _2: SimpleColumn[T2],
  override val _3: SimpleColumn[T3],
  override val _4: SimpleColumn[T4],
  override val _5: SimpleColumn[T5])
extends Tuple5(_1,_2,_3,_4,_5) with Projection[(T1,T2,T3,T4,T5)] {
  def ~[U](c: SimpleColumn[U]) = new Projection6(_1,_2,_3,_4,_5,c)
  def getResult(rs: PositionedResult) =
    (_1.getResult(rs),
     _2.getResult(rs),
     _3.getResult(rs),
     _4.getResult(rs),
     _5.getResult(rs))
}


final class Projection6[T1,T2,T3,T4,T5,T6](
  override val _1: SimpleColumn[T1],
  override val _2: SimpleColumn[T2],
  override val _3: SimpleColumn[T3],
  override val _4: SimpleColumn[T4],
  override val _5: SimpleColumn[T5],
  override val _6: SimpleColumn[T6])
extends Tuple6(_1,_2,_3,_4,_5,_6) with Projection[(T1,T2,T3,T4,T5,T6)] {
  def ~[U](c: SimpleColumn[U]) = new Projection7(_1,_2,_3,_4,_5,_6,c)
  def getResult(rs: PositionedResult) =
    (_1.getResult(rs),
     _2.getResult(rs),
     _3.getResult(rs),
     _4.getResult(rs),
     _5.getResult(rs),
     _6.getResult(rs))
}

final class Projection7[T1,T2,T3,T4,T5,T6,T7](
  override val _1: SimpleColumn[T1],
  override val _2: SimpleColumn[T2],
  override val _3: SimpleColumn[T3],
  override val _4: SimpleColumn[T4],
  override val _5: SimpleColumn[T5],
  override val _6: SimpleColumn[T6],
  override val _7: SimpleColumn[T7])
extends Tuple7(_1,_2,_3,_4,_5,_6,_7) with Projection[(T1,T2,T3,T4,T5,T6,T7)] {
  def ~[U](c: SimpleColumn[U]) = new Projection8(_1,_2,_3,_4,_5,_6,_7,c)
  def getResult(rs: PositionedResult) =
    (_1.getResult(rs),
     _2.getResult(rs),
     _3.getResult(rs),
     _4.getResult(rs),
     _5.getResult(rs),
     _6.getResult(rs),
     _7.getResult(rs))
}

final class Projection8[T1,T2,T3,T4,T5,T6,T7,T8](
  override val _1: SimpleColumn[T1],
  override val _2: SimpleColumn[T2],
  override val _3: SimpleColumn[T3],
  override val _4: SimpleColumn[T4],
  override val _5: SimpleColumn[T5],
  override val _6: SimpleColumn[T6],
  override val _7: SimpleColumn[T7],
  override val _8: SimpleColumn[T8])
extends Tuple8(_1,_2,_3,_4,_5,_6,_7,_8) with Projection[(T1,T2,T3,T4,T5,T6,T7,T8)] {
  def ~[U](c: SimpleColumn[U]) = new Projection9(_1,_2,_3,_4,_5,_6,_7,_8,c)
  def getResult(rs: PositionedResult) =
    (_1.getResult(rs),
     _2.getResult(rs),
     _3.getResult(rs),
     _4.getResult(rs),
     _5.getResult(rs),
     _6.getResult(rs),
     _7.getResult(rs),
     _8.getResult(rs))
}

final class Projection9[T1,T2,T3,T4,T5,T6,T7,T8,T9](
  override val _1: SimpleColumn[T1],
  override val _2: SimpleColumn[T2],
  override val _3: SimpleColumn[T3],
  override val _4: SimpleColumn[T4],
  override val _5: SimpleColumn[T5],
  override val _6: SimpleColumn[T6],
  override val _7: SimpleColumn[T7],
  override val _8: SimpleColumn[T8],
  override val _9: SimpleColumn[T9])
extends Tuple9(_1,_2,_3,_4,_5,_6,_7,_8,_9) with Projection[(T1,T2,T3,T4,T5,T6,T7,T8,T9)] {
  //def ~[U](c: SimpleColumn[U]) = new Projection10(_1,_2,_3,_4,_5,_6,_7,_8,_9,c)
  def getResult(rs: PositionedResult) =
    (_1.getResult(rs),
     _2.getResult(rs),
     _3.getResult(rs),
     _4.getResult(rs),
     _5.getResult(rs),
     _6.getResult(rs),
     _7.getResult(rs),
     _8.getResult(rs),
     _9.getResult(rs))
}

1 个答案:

答案 0 :(得分:0)

如何使用 sbt-boilerplate https://github.com/sbt/sbt-boilerplate)并编写如下模板:

trait PositionedResult
trait SimpleColumn[T]{
  def getResult(rs: PositionedResult) = ???
}
trait Projection[T]

[#final class Projection1[[#T1#]]([#override val _1: SimpleColumn[T1]#]) extends Tuple1([#_1#]) with Projection[([#T1#])]{
  def ~[U](c: SimpleColumn[U]) = Projection1.~(this, c)
  def getResult(rs: PositionedResult) = ([#_1.getResult(rs)#])
}#
]

[1..21#object Projection1{
  def ~[U, [#T1#]](p: Projection1[[#T1#]], c: SimpleColumn[U]) = new Projection2([#p._1#], c)
}#
]

object Projection22{
  def ~[U, T](p: Projection[T], c: SimpleColumn[U]) = ???
}