Skip to content

Instantly share code, notes, and snippets.

@ckirkendall
Created September 24, 2012 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckirkendall/3778515 to your computer and use it in GitHub Desktop.
Save ckirkendall/3778515 to your computer and use it in GitHub Desktop.
Showing how implicits can demonstrate contra and co variance with out all the type noise.
package example
class Widget(val description : String)
class CoolWidget extends Widget("cool widget")
class Sprocket(val description : String)
object Sprocket {
def printDescription(s : Sprocket){
println(s.description)
}
}
class CoolSprocket(override val description : String) extends Sprocket(description)
object Main extends App {
implicit def widgetToCoolSprocket(w: Widget) : CoolSprocket ={
return new CoolSprocket(w.description);
}
var myCoolWidget = new CoolWidget();
Sprocket.printDescription(myCoolWidget);
println(Lists.max(List(1,3,2)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment