Skip to content

Instantly share code, notes, and snippets.

@timperrett
Created April 8, 2011 23:14
Show Gist options
  • Save timperrett/910915 to your computer and use it in GitHub Desktop.
Save timperrett/910915 to your computer and use it in GitHub Desktop.
Example of using Scala implicits to do CssSel type classes
package eu.getintheloop.snippet
// Author: Timothy Perrett
// Date: 11th April 2011
case class Product(name: String, price: Long)
import net.liftweb.util.CssSel
object Bindings {
class Bind[T](what: T){
def bind(implicit bind: DataBinding[T]) = bind(what)
}
type DataBinding[T] = T => CssSel
implicit def asCssSelector[T](in : T): Bind[T] = new Bind[T](in)
}
import Bindings._
import net.liftweb.util.Helpers._
object ProductBinding extends DataBinding[Product]{
def apply(product: Product): CssSel =
".name" #> product.name
}
class MySnippet {
private implicit val binder = ProductBinding
def render: CssSel = Product("whatever", 1234L).bind
// alternative syntax (but requires manual wiring):
// def render[Product : Bind] = Product("whatever", 1234L).bind
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment