Pregunta

Actualmente tengo

  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>
    }
  }

y traté de refactorearlo a

  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>
    }
  }

pero me sale el siguiente. He rastreado todos los tipos de retorno y no veo cómo mi refactorización es diferente de lo que se ocurre internamente. Incluso he intentado añadir más casos de los partidos (como se puede ver en el código refactorizado) para asegurarse de que estaba seleccionando el tipo correcto.

/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>
                                                                  ^
¿Fue útil?

Solución

Así es como mi cerebro analiza el mensaje de error ...

error: overloaded method value ->

Este es el nombre del método, que es '->'.

with alternatives 

Lo que sigue es la lista de posibles parámetros para -> dentro de la función bind ().

[T <: net.liftweb.util.Bindable](T with net.liftweb.util.Bindable)net.liftweb.util.Helpers.TheBindableBindParam[T] 

Esto dice que cualquier cosa que implementa o incluye el rasgo enlazables es juego limpio.

<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 

El manojo de opciones específicas del 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! Nodo-cosas relacionadas. Nuestras opciones válidas parecen ser NodeSeq, Sec [nodo], texto, y el nodo

cannot be applied to (List[scala.xml.NodeSeq])

Parece que la lista [NodeSeq] no es una opción válida.

Con esto en mente, es probable que desee tomar un NodeSeq individuo fuera de la lista a fin de obligar a la forma. ¿Está seguro de que realmente desea devolver una lista en el método de ayuda?

Otros consejos

No pude ver que se extiende NodeSeq Sec [nodo], así que tenía el tipo de retorno incorrecto en el método extraído. Si lo cambia a

  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.

Uno de los problemas es que su partido no tiene mucho sentido: básicamente, que son los mismos ya sea contra una lista vacía o una lista no vacía. No hay otra posibilidad:

all match {
  case Nil          =>  //if list is empty
  case nonEmptyList =>  //if list is not empty
}

Por supuesto que también podría hacer:

case Nil       =>
case x :: Nil  => //list with only a head
case x :: xs   => //head :: tail

Como nota al margen, hay una cosa en su código que no funciona:

case all: List[NodeSeq]

Debido a tipo de borrado , no hay manera de probar, en tiempo de ejecución, si all anota el List[NodeSeq], List[String], List[AnyRef] o lo que usted quiera. Estoy bastante seguro de que debe estar recibiendo una advertencia en esa línea, y haciendo caso omiso porque no entiende lo que le está advirtiendo sobre (al menos, eso es lo que me pasó cuando llegué a tal advertencia :). La línea correcta sería:

case all: List[_]

¿Cuál sería aceptar cualquier tipo de List. Busque una pregunta mía sobre el tipo de borrado y la Scala de ver un poco más sobre ello, si usted está interesado.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top