Question

J'ai actuellement

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

et j'ai essayé de le refactoriser à

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

mais je reçois ce qui suit. J'ai retracé tous les types de retour et je ne vois pas comment mon refactoring est différent de ce qui se passerait en interne. J'ai même essayé d'ajouter plus de cas de match (comme vous pouvez le voir dans le code refondus) pour vous assurer que je choisissais le bon type.

/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>
                                                                  ^
Était-ce utile?

La solution

Voici comment mon cerveau analysé le message d'erreur ...

error: overloaded method value ->

est le nom de la méthode, qui est « -> ».

with alternatives 

Ce qui va suivre est la liste des paramètres possibles pour -> au sein de la fonction bind ().

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

Ceci indique que tout ce qui met en œuvre ou inclut le trait Bindable est un jeu équitable.

<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 

Bunch d'options spécifiques de type.

<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! choses associées à un noeud. Nos options valides semblent être NodeSeq, Seq [nœud], texte et nœud

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

On dirait Liste [NodeSeq] n'est pas une option valable.

Avec cela à l'esprit, vous voulez probablement prendre une NodeSeq individuelle de la liste afin de le lier à la forme. Etes-vous sûr que vous voulez vraiment retourner une liste de la méthode d'aide?

Autres conseils

Je pas voir qui va NodeSeq Seq [nœud], donc j'eu le mauvais type de retour sur la méthode extraite. il passer à

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

.

Un problème est que votre match n'a pas vraiment de sens: fondamentalement, vous êtes correspondant à contre soit une liste vide ou une liste non vide. Il n'y a pas d'autre possibilité:

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

Bien sûr, vous pouvez également faire:

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

Comme une note de côté, il y a une chose dans votre code qui ne fonctionne pas:

case all: List[NodeSeq]

En raison de effacement de type , il n'y a aucun moyen de tester, à l'exécution, si la liste all un List[NodeSeq], List[String], List[AnyRef] ou ce que vous voudrez. Je suis sûr que vous obtiendrez un avertissement sur cette ligne, et en ignorant parce que vous ne comprenez pas ce qu'il vous avertit au sujet (au moins, ce qui est arrivé à moi quand je suis arrivé cet avertissement :). La ligne correcte serait:

case all: List[_]

Ce qui accepterait tout type de List. Recherchez une question de mine sur l'effacement de type et Scala pour voir un peu plus à ce sujet, si vous êtes intéressé.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top