Scala koan AboutExtractors.scala错了吗?

时间:2016-05-27 03:10:32

标签: scala

我对AboutExtractors.scala中的这个公案感到有点困惑。这段代码来自 https://bitbucket.org/dmarsh/scalakoansexercises/wiki/Home

koan("In this koan we use the unapply for pattern matching employee objects") {

class Employee(val firstName: String,
               val middleName: Option[String],
               val lastName: String)

object Employee {
  //factory methods, extractors, apply
  //Extractor: Create tokens that represent your object
  def unapply(x: Employee) =
    Some(x.lastName, x.middleName, x.firstName)
}

val singri = new Employee("Singri", None, "Keerthi")

val result = singri match {
  case Employee("Singri", None, x) => "Yay, Singri %s! with no middle name!".format(x)
  case Employee("Singri", Some(x), _) => "Yay, Singri with a middle name of %s".format(x)
  case _ => "I don't care, going on break"
}

result should be("I don't care, going on break")
}

为什么不调用行case Employee("Singri", None, x) => "Yay, Singri %s! with no middle name!".format(x)

在我看来Employee("Singri", None, "Keerthi")匹配Employee("Singri", None, x)

1 个答案:

答案 0 :(得分:0)

提取器(错误/不可取)更改类参数的顺序,将姓氏放在第一位。因此.atpcontent{ background-color:#bbb; padding:10px 10px; font-size:1em; } .atpcontent h2{ padding:0; font-family:'raleway', arial, sans-serif; font-size:1.5em; font-weight:600; color:#0e4373; position: relative; display: inline-block; } .atpcontent p{ padding:7px 0; color:#0e4373; font-size:1em; font-family:'raleway',arial,sans-serif;} .atpcontent img{ width:100%; height:auto;} .atpcontent a{ text-decoration:none; font-weight:bold; color:inherit; transition:1s;} .atpcontent a:hover{ color:#06C; transition:1s; } .ajdrop{word-wrap:break-word;padding-bottom:10px;} .ajdrop p{ padding:7px 0; font-family:'raleway', arial, sans-serif; color:#0e4373; font-size:1em;} /* dropdown arrow sections css */ .ajdrop h2{ background: url(assets/img/down.png) no-repeat 0 12px; /*comment this if you dont want arrows*/ padding:9px 0 0 20px; font-family:'raleway', arial, sans-serif; font-size:20px; color:#0e4373;} .ajdrop h2.close{background-image: url(assets/img/up.png); }/*comment this if you dont want arrows*/ .aanswer{margin-left:35px;} 会匹配。