题
我现在有
def list(node: NodeSeq): NodeSeq = {
val all = Thing.findAll.flatMap({
thing => bind("thing", chooseTemplate("thing", "entry", node),
"desc" -> Text(thing.desc.is),
"creator" -> thing.creatorName.getOrElse("UNKNOWN"),
"delete" -> SHtml.link("/test", () => delete(thing), Text("delete"))
)
})
all match {
case Nil => <span>No things</span>
case _ => <ol>{bind("thing", node, "entry" -> all)}</ol>
}
}
和我试图将其重构至
def listItemHelper(node: NodeSeq): List[NodeSeq] = {
Thing.findAll.flatMap({
thing => bind("thing", chooseTemplate("thing", "entry", node),
"desc" -> Text(thing.desc.is),
"creator" -> thing.creatorName.getOrElse("UNKNOWN"),
"delete" -> SHtml.link("/test", () => delete(thing), Text("delete"))
)
})
}
def list(node: NodeSeq): NodeSeq = {
val all = listItemHelper(node)
all match {
case Nil => <span>No things</span>
case all: List[NodeSeq] => <ol>{bind("thing", node, "entry" -> all)}</ol>
case _ => <span>wtf</span>
}
}
但我得到以下。我跟踪所有的返回类型,我不看我的重构是如何比什么是内部发生任何不同。我甚至尝试增加更多的比赛的情况下(你可以在重构代码中看到),以确保我选择合适的类型。
/Users/trenton/projects/sc2/supperclub/src/main/scala/com/runbam/snippet/Whyme.scala:37: error: overloaded method value -> with alternatives [T <: net.liftweb.util.Bindable](T with net.liftweb.util.Bindable)net.liftweb.util.Helpers.TheBindableBindParam[T] <and> (Boolean)net.liftweb.util.Helpers.BooleanBindParam <and> (Long)net.liftweb.util.Helpers.LongBindParam <and> (Int)net.liftweb.util.Helpers.IntBindParam <and> (Symbol)net.liftweb.util.Helpers.SymbolBindParam <and> (Option[scala.xml.NodeSeq])net.liftweb.util.Helpers.OptionBindParam <and> (net.liftweb.util.Box[scala.xml.NodeSeq])net.liftweb.util.Helpers.BoxBindParam <and> ((scala.xml.NodeSeq) => scala.xml.NodeSeq)net.liftweb.util.Helpers.FuncBindParam <and> (Seq[scala.xml.Node])net.liftweb.util.Helpers.TheBindParam <and> (scala.xml.Node)net.liftweb.util.Helpers.TheBindParam <and> (scala.xml.Text)net.liftweb.util.Helpers.TheBindParam <and> (scala.xml.NodeSeq)net.liftweb.util.Helpers.TheBindParam <and> (String)net.liftweb.util.Helpers.TheStrBindParam cannot be applied to (List[scala.xml.NodeSeq])
case all: List[NodeSeq] => <ol>{bind("thing", node, "entry" -> all)}</ol>
^
解决方案
下面是我的大脑如何解析错误消息...
error: overloaded method value ->
这是该方法,这是的名称“ - >”。
with alternatives
什么将遵循是可能的参数的列表 - 绑定()函数内>。
[T <: net.liftweb.util.Bindable](T with net.liftweb.util.Bindable)net.liftweb.util.Helpers.TheBindableBindParam[T]
这表示,任何其实现或包括该性状可绑定是公平的游戏。
<and> (Boolean)net.liftweb.util.Helpers.BooleanBindParam
<and> (Long)net.liftweb.util.Helpers.LongBindParam
<and> (Int)net.liftweb.util.Helpers.IntBindParam
<and> (Symbol)net.liftweb.util.Helpers.SymbolBindParam
<and> (Option[scala.xml.NodeSeq])net.liftweb.util.Helpers.OptionBindParam
<and> (net.liftweb.util.Box[scala.xml.NodeSeq])net.liftweb.util.Helpers.BoxBindParam
的特定类型的选择一束。
<and> ((scala.xml.NodeSeq) => scala.xml.NodeSeq)net.liftweb.util.Helpers.FuncBindParam
<and> (Seq[scala.xml.Node])net.liftweb.util.Helpers.TheBindParam
<and> (scala.xml.Node)net.liftweb.util.Helpers.TheBindParam
<and> (scala.xml.Text)net.liftweb.util.Helpers.TheBindParam
<and> (scala.xml.NodeSeq)net.liftweb.util.Helpers.TheBindParam
<and> (String)net.liftweb.util.Helpers.TheStrBindParam
啊!节点相关的东西。我们的有效选项似乎是NodeSeq,SEQ [节点],Text和节点
cannot be applied to (List[scala.xml.NodeSeq])
看起来像列表[NodeSeq]不是有效的选项。
考虑到这一点,你可能要采取个人NodeSeq淘汰之列,以将其绑定到表单。你确定你真的想从辅助方法返回一个列表?
其他提示
我没有看到NodeSeq延伸SEQ [节点],所以对提取出的方法的错误返回类型。其改为
def listItemHelper(node: NodeSeq): NodeSeq = {
Thing.findAll.flatMap({
thing => bind("thing", chooseTemplate("thing", "entry", node),
"desc" -> Text(thing.desc.is),
"creator" -> thing.creatorName.getOrElse("UNKNOWN"),
"delete" -> SHtml.link("/test", () => delete(thing), Text("delete"))
)
})
}
def list(node: NodeSeq): NodeSeq = {
val all = listItemHelper(node)
all.length match {
case 0 => <span>No things</span>
case _ => <ol>{bind("thing", node, "entry" -> all)}</ol>
}
}
作品。
一个问题是,你的对手并没有真正使任何意义:基本上你是对的匹配一个空列表或一个非空列表。没有其他的可能性:
all match {
case Nil => //if list is empty
case nonEmptyList => //if list is not empty
}
当然,你也可以这样做:
case Nil =>
case x :: Nil => //list with only a head
case x :: xs => //head :: tail
作为一个侧面说明,有一件事在你的代码不工作:
case all: List[NodeSeq]
由于类型擦除的,有没有办法测试,在运行时,是否all
列出List[NodeSeq]
,List[String]
,List[AnyRef]
或什么都有,你。我敢肯定,你一定是在该行警告,忽略它,因为你不知道它是什么警告你(至少,这就是发生在我身上时,我得到这样的警告:)。正确的路线将是:
case all: List[_]
这将接受任何形式的List
的。查一查类型擦除和Scala我的一个问题,看多一点吧,如果你有兴趣。
不隶属于 StackOverflow