views:

129

answers:

4

I currently have

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

and I tried to refactor it to

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

but I get the following. I've traced all the return types and I don't see how my refactoring is any different than what would be happening internally. I even tried adding more match cases (as you can see in the refactored code) to make sure I was selecting the right 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>
                                                                  ^
+1  A: 

I failed to see that NodeSeq extends Seq[Node], so I had the wrong return type on the extracted method. Changing it to

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

works.

trenton
+1  A: 

One issue is that your match doesn't really make any sense: basically you are matching against either an empty list or a non-empty list. There is no other possibility:

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

Of course you could also do:

case Nil       =>
case x :: Nil  => //list with only a head
case x :: xs   => //head :: tail
oxbow_lakes
+3  A: 

Here's how my brain parsed the error message...

error: overloaded method value ->

This is the name of the method, which is '->'.

with alternatives

What will follow is the list of possible parameters for -> within the bind() function.

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

This says that anything which implements or includes the trait Bindable is fair game.

<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 of type-specific options.

<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-related stuff. Our valid options seem to be NodeSeq, Seq[Node], Text, and Node

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

Looks like List[NodeSeq] is not a valid option.

With this in mind, you probably want to take an individual NodeSeq out of the List in order to bind it to the form. Are you sure you really want to return a List from the helper method?

Benjamin Cox
+1  A: 

As a side note, there's one thing in your code that doesn't work:

case all: List[NodeSeq]

Because of type erasure, there's no way to test, at runtime, whether all list a List[NodeSeq], List[String], List[AnyRef] or what-have-you. I'm pretty sure you must be getting a warning on that line, and ignoring it because you don't understand what it is warning you about (at least, that's what happened to me when I got such warning :). The correct line would be:

case all: List[_]

Which would accept any kind of List. Look up a question of mine on type erasure and Scala to see a bit more about it, if you are interested.

Daniel
There was a compile error earlier on, so no warning b/c of that.
trenton