相关疑难解决方法(0)

如何模式匹配具有多个参数列表的类?

考虑这个课程:

class DateTime(year: Int, month: Int, day: Int)(hour: Int, minute: Int, second: Int)
Run Code Online (Sandbox Code Playgroud)

unapply方法将如何,如果我想匹配以下内容:

dt match {
  case DateTime(2012, 12, 12)(12, _, _) => // December 12th 2012, 12 o'clock
  /* ... */
}
Run Code Online (Sandbox Code Playgroud)

我试过这个:

def unapply(dt: DateTime) = 
  Some((dt.year, dt.month, dt.day),(dt.hour, dt.minute, dt.second))
Run Code Online (Sandbox Code Playgroud)

但这并没有真正起作用.

scala class case pattern-matching unapply

19
推荐指数
2
解决办法
3236
查看次数

标签 统计

case ×1

class ×1

pattern-matching ×1

scala ×1

unapply ×1