【发布时间】:2012-08-04 06:11:04
【问题描述】:
目前我有几个非常相似的方法,我想将它们合并为一种方法。这里有两种方法
def toInt(attrType: String, attrValue: String): Int = {
attrType match {
case "N" => attrValue.toInt
case _ => -1
}
}
def toString(attrType: String, attrValue: String): String = {
attrType match {
case "S" => attrValue
case _ => ""
}
}
我认为在 Scala 中使用泛型有更简单的方法吗?
【问题讨论】:
-
问题是:你想简化什么?在这个例子中,唯一有重复代码的是
attrType match {。其余的要不同以使其更通用。 -
实际代码比我展示的要复杂,我为问题简化了。
标签: scala