【问题标题】:Constructor cannot be instantiated to expected type; p @ Person构造函数无法实例化为预期类型; p@人
【发布时间】:2013-02-06 10:06:57
【问题描述】:

我正在使用 scala 版本:Scala code runner version 2.9.2-unknown-unknown -- Copyright 2002-2011, LAMP/EPFL

我从这里尝试深度大小写匹配构造:http://ofps.oreilly.com/titles/9780596155957/RoundingOutTheEssentials.html,代码如下match-deep.scala

class Role
case object Manager extends Role
case object Developer extends Role

case class Person(name:String, age: Int, role: Role)

val alice = new Person("Alice", 25, Developer)
val bob = new Person("Bob", 32, Manager)
val charlie = new Person("Charlie", 32, Developer)

for( person <- List(alice, bob, charlie) ) {
  person match {
    case (id, p @ Person(_, _, Manager)) => println("%s is overpaid".format(p))
    case (id, p @ Person(_, _, _)) => println("%s is underpaid".format(p))
  }
}

我收到以下错误:

match-deep.scala:13: error: constructor cannot be instantiated to expected type;
 found   : (T1, T2)
 required: this.Person
    case (id, p @ Person(_, _, Manager)) => println("%s is overpaid".format(p))
         ^
match-deep.scala:13: error: not found: value p
    case (id, p @ Person(_, _, Manager)) => println("%s is overpaid".format(p))
                                                                            ^
match-deep.scala:14: error: constructor cannot be instantiated to expected type;
 found   : (T1, T2)
 required: this.Person
    case (id, p @ Person(_, _, _)) => println("%s is underpaid".format(p))
         ^
match-deep.scala:14: error: not found: value p
    case (id, p @ Person(_, _, _)) => println("%s is underpaid".format(p))

我在这里做错了什么?

【问题讨论】:

  • 知道在 case 语句中添加 'id' 的目的是什么吗?也许它与旧的 Scala 语法有关?

标签: scala scala-2.9


【解决方案1】:

错误信息一目了然

for( person <- List(alice, bob, charlie) ) {
  person match {
    case p @ Person(_, _, Manager) => println("%s is overpaid".format(p.toString))
    case p @ Person(_, _, _) => println("%s is underpaid".format(p.toString))
  }
}

这是做同样事情的一个简短方法:

for(p @ Person(_, _, role) <- List(alice, bob, charlie) ) {
  if(role == Manager) println("%s is overpaid".format(p.toString))
  else println("%s is underpaid".format(p.toString))
}

编辑 我不确定您想要的id 的真正含义是什么,我想它是列表中的人的index。我们开始:

scala> for((p@Person(_,_,role), id) <- List(alice, bob, charlie).zipWithIndex ) {
     |   if(role == Manager) printf("%dth person is overpaid\n", id)
     |   else printf("Something else\n")
     | }
Something else
1th person is overpaid
Something else

【讨论】:

  • 您好 Eastsun!感谢您的回答:) 请检查我在上面发布的问题中的评论?
【解决方案2】:

错误是因为您在person 上的匹配不是(id,person) 的元组

person match{
  case p @ Person(_, _, Manager)) => println("%s is overpaid".format(p)
  case p : Person => println("%s is underpaid".format(p.toString))
}

这应该足够了。

编辑

您可以调整它以轻松使用foreach

List(alice, bob, charlie) foreach {
    case p@Person(_,_,Manager)=>println("%s is overpaid".format(p);
    case p:Person =>println("%s is underpaid".format(p.toString)) 
}

【讨论】:

  • 你好,korefn!谢谢你的回答。这真的很有帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-18
相关资源
最近更新 更多