2

An example from lift cookbook, the pattern matching is bit curious here.

serve("issues" / "by-state" prefix { case "open" :: Nil XmlGet _ => <p>None open</p> case "closed" :: Nil XmlGet _ => <p>None closed</p> case "closed" :: Nil XmlDelete _ => <p>All deleted</p> }) 

I don't understand what the XmlGet _ part is doing. Could anyone explain a bit?

2
  • it's some kind of partial function. I would suggest got to RuleHelper class and looks for hidden implicits there. Commented Oct 29, 2015 at 23:58
  • answered in the gitter room. It's the same syntax as _ :: Nil. Commented Oct 30, 2015 at 0:17

2 Answers 2

4

One of Scala's nice niche features is that many binary operations (e. g. f(x, y)) can be invoked from the infix position x f y. This applies to normal method calls:

case class InfixMethodCalls(x: Int) { def wild(y: Int): Int = x + y } val infix = InfixMethodCalls(3) infix wild 4 

type constructors:

// A simple union type based on http://www.scalactic.org/ trait Or[A, B] case class Good[A, B](value: A) extends Or[A, B] case class Bad[A, B](value: B) extends Or[A, B] def myMethod(x: Int Or String): Int // This is the same as def myMethod(x: Or[Int, String]): Int 

and unapply / unapplySeq:

object InfixMagic { def unapply(x: Any) = Option((List(x), x)) } 123 match { case v :: Nil InfixMagic x => println(s"got v: $v and x: $x") } // is the same as 123 match { case InfixMagic(v :: Nil, x) => println(s"got v: $v and x: $x") } 

So in the case of XmlGet this syntax here:

case "open" :: Nil XmlGet _ => 

is the same as:

case XmlGet("open" :: Nil, _) => 

And the _ is ignoring the Req parameter, which is the second part of the returned value from TestGet.unapply.

Sign up to request clarification or add additional context in comments.

Comments

1

If you go through RuleHelper class of liftweb framework one will be able to make few assumptions.

  1. XmlGet and XmlDelete extends TestGet trait with unapply method and Request argument. So this part basically means: check if it's XmlGet\XmlDelete method with any request.

  2. How list is separated from second part? Good question. Suppose implicit listStringToSuper and listServeMagic used for this purpose.

https://github.com/lift/framework/blob/master/web/webkit/src/main/scala/net/liftweb/http/rest/RestHelper.scala

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.