Created
          April 8, 2011 23:14 
        
      - 
      
 - 
        
Save timperrett/910915 to your computer and use it in GitHub Desktop.  
    Example of using Scala implicits to do CssSel type classes 
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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