考虑下面这个类:
class MyClass {
public int myInt;
public string myString;
}
class DateTime(year: Int, month: Int, day: Int)(hour: Int, minute: Int, second: Int)
如果我想匹配像这样的内容,那么unapply
方法会是什么样子:
dt match {
case DateTime(2012, 12, 12)(12, _, _) => // December 12th 2012, 12 o'clock
/* ... */
}
我尝试了这个:
def unapply(dt: DateTime) =
Some((dt.year, dt.month, dt.day),(dt.hour, dt.minute, dt.second))
但那真的没有起作用。
case class MyCase[A <: WithBar](a: A)(b: A#Bar)
并且从中获得所有 "nifty features",那么就可以像val x = MyCase(FooWithBar(...))(Bar(...))
这样创建新实例,而不需要为A
进行类型注释。使用MyCase[A <: WithBar](a: A, b: A#Bar)
这样的签名,你必须用val x = MyCase[FooWithBar](FooWithBar(...), Bar(...))
实例化,因为当在与类型A
的参数列表中指定A#Bar
参数时,类型推断无法工作。 - falconepl