Вопрос

В анорме Play2 есть хороший парсер результатов DSL:

родовое слово

Я не понимаю, как эта часть создает кодовый код, почему между двумя переменными может быть общий кодовый код?

Обычно я вижу код генерирующего кода:

родовое слово

Но я не вижу общего кода тега

Источник кода case id ~ name находится здесь: https://github.com/playframework/Play20/blob/master/framework/src/anorm/src/main/scala/SqlParser.scala#L49

Он определяет класс case ~:

родовое слово

И я пишу простой тест:

родовое слово

Он печатает case, но почему используется синтаксис case id ~ name?

Это было полезно?

Решение

You're already halfway there. It is possible because Scala lets you do that for all types that have been declared with two type parameters.

For example:

scala> case class Foo[X,Y]()
defined class Foo

scala> val x: Int Foo Double = Foo[Int,Double]()
x: Foo[Int,Double] = Foo()

While it may seem odd at first, it's actually a really nice property as this syntax can make things more readable. Consider the following example where a custom type for a tuple is defined:

class |::|[A, B](val left: A, val right: B)

object |::| {
  def unapply[A, B](o: A |::| B) = Some((o.left, o.right))
}

Here, A |::| B is used as infix notation for |::|[A, B]. On the other hand, scala also allows infix notation for pattern matching (thanks to incrop for the reminder), as in case of the constructor in the following example:

new |::|("Hello","World") match {
  case l |::| r => Console println (l + "," + r)
  case _ =>
}  
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top