Created
September 29, 2011 13:43
-
-
Save adamrabung/1250746 to your computer and use it in GitHub Desktop.
another approach for typeclass examples here: http://skipoleschris.blogspot.com/2011/09/type-classes-and-bounds-in-scala.html
This file contains 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
trait Publishable[T] { | |
def asWebMarkup(p: T): String | |
} | |
case class BlogPost(title: String, text: String) | |
implicit object BlogPostPublisher extends Publishable[BlogPost] { | |
def asWebMarkup(p: BlogPost) = | |
"""|<h2>%s</h2> | |
|<div>%s</div>""".stripMargin format(p title, p text) | |
} | |
object WebPublisher { | |
def publish[T](p: T)(implicit publisher:Publishable[T]) = println(publisher.asWebMarkup(p)) | |
} | |
val post = new BlogPost("Test", "This is a test post") | |
val web = WebPublisher.publish(post) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment