gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:24:34 24 4
gpt4 key购买 nike

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

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))
}

最佳答案

使用 怎么样? sbt-样板 ( 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]) = ???
}

关于scala - 在scala中,如何为不同长度的参数列表生成样板代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30166794/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com