Refatoração val ao método resulta em erro de compilação
-
19-09-2019 - |
Pergunta
Eu tenho atualmente
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>
}
}
e eu tentei refatorar-lo para
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>
}
}
mas eu recebo o seguinte. Eu já traçou todos os tipos de retorno e eu não vejo como minha refatoração é diferente do que o que estaria acontecendo internamente. Eu até tentei adicionar mais casos jogo (como você pode ver no código refatorado) para se certificar de que eu estava selecionando o tipo certo.
/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>
^
Solução
Veja como meu cérebro analisado a mensagem de erro ...
error: overloaded method value ->
Este é o nome do método, que é '->'.
with alternatives
O que vai seguir a lista de possíveis parâmetros para -> dentro da função bind ().
[T <: net.liftweb.util.Bindable](T with net.liftweb.util.Bindable)net.liftweb.util.Helpers.TheBindableBindParam[T]
Este diz que qualquer coisa que implementa ou inclui o traço Bindable é um jogo justo.
<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
Grupo de opções específicas do tipo.
<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
Ah! Node-relacionado coisas. Nossas opções válidas parecem ser NodeSeq, Seq [Nó], Texto e Node
cannot be applied to (List[scala.xml.NodeSeq])
Looks como Lista [NodeSeq] não é uma opção válida.
Com isto em mente, você provavelmente vai querer ter um NodeSeq indivíduo fora da lista, a fim de vinculá-lo ao formulário. Tem certeza de que quer realmente retornar uma lista do método auxiliar?
Outras dicas
Eu não conseguiram ver que NodeSeq estende Seq [Nó], então eu tinha o tipo de retorno errado no método extraído. Mudá-lo para
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>
}
}
obras.
Uma questão é que seu jogo realmente não faz qualquer sentido: basicamente, você está combinando contra qualquer uma lista vazia ou uma lista não-vazia. Não há outra possibilidade:
all match {
case Nil => //if list is empty
case nonEmptyList => //if list is not empty
}
É claro que você também pode fazer:
case Nil =>
case x :: Nil => //list with only a head
case x :: xs => //head :: tail
Como uma nota lateral, há uma coisa no seu código que não funciona:
case all: List[NodeSeq]
Por causa de tipo de apagamento , não há nenhuma maneira de testar, em tempo de execução, quer lista all
um List[NodeSeq]
, List[String]
, List[AnyRef]
ou que-ter-você. Tenho certeza que você deve estar recebendo um aviso nessa linha, e ignorando-o porque você não entende o que está a avisá-lo sobre (pelo menos, foi o que aconteceu comigo quando eu tenho esse aviso :). A linha correta seria:
case all: List[_]
O que aceitaria qualquer tipo de List
. Olhe-se uma questão de mina do tipo de apagamento e Scala para ver um pouco mais sobre isso, se você estiver interessado.