0
其實,我寫了一個scala腳本,它正在根據某個參數對函數進行排序。看下面的代碼片段:如何重構scala代碼?
object TestTypes {
type SortFn = ((String, TestMetricFields)) => Double
case class TestMetricFields(maths: Int, science: Int)
}
trait TestMarks {
def sName: String
def getSortFun(): SortFn
}
object TestMarkStudent extends TestMarks {
override def sName: String= "xxx"
override def getSortFun(): SortFn = {
input: (String, TestMetricFields) => input._2.maths + input._2.science
}
}
這裏我想避免在上面的代碼中使用._2
。如何重寫Scala代碼?
我試過以下,但我失敗了。
override def getSortFun(): (String, TestMetricFields) => SortFn = {
(sName: String, testField: TestMetricFields) => {
input: (String, TestMetricFields) => 1.0 * testField.maths + testField.science
}
}
誰能幫我重構代碼,而無需使用._2
?
你有什麼要避免使用'_2'? – Todd