我使用https://pureconfig.github.io/来加载配置值。例如,对于数据库中的每个表,我存储(db: String, table: String)
. 但是,我需要表示特定的表。因此,每个人都有不同的特质。IE:
trait Thing
trait ThingWithStuff extends Thing {
def value:String
}
trait FooThing extends Thing{
def fooThing: ThingWithStuff
}
trait BarThing extends Thing{
def barThing: ThingWithStuff
}
它们都有一个不同的属性名称,具有相同的类型,作为回报,它们拥有 iedb
和table
. 使用某些方法处理这些时:
def myMethodFoo(thing:FooThing)= println(thing.fooThing)
def myMethodBar(thing:BarThing)= println(thing.barThing)
它会导致代码重复。尝试使用泛型解决这些问题,我无法编写如下函数:
def myMethod[T<: Thing] = println(thing.thing)
因为属性名称会不同。有没有聪明的方法?笔记:
table-first {
db = "a"
table = "b"
}
table-second {
db = "foo"
table = "baz"
}
前面不能有相同的标识符,否则它将覆盖每个值以仅保存此标识符的最后一项的值。因此,我求助于使用不同的属性名称(table-first, table-second
或专门用于示例fooThing, barThing
:)
如何解决此问题以防止代码重复?