type classes in Scala - a minimal self-contained example -



type classes in Scala - a minimal self-contained example -

i looked @ examples type class scenarios , how imitate haskell-ish concept in scala, e.g. @ http://danielwestheide.com/blog/2013/02/06/the-neophytes-guide-to-scala-part-12-type-classes.html. think much involved , narrative-laden be. can provide authoritative minimal illustration polymorphism using type classes rather inheritance , mixins?

thanks!

// type class trait show[a] { def show(x: a): string } // usage def greet[a](x: a)(implicit sh: show[a]) = s"hello ${sh.show(x)}" // illustration instance implicit object showdouble extends show[double] { def show(x: double) = f"$x%1.3f" // format 3 digits } greet(math.pi) // "hello 3.142"

scala

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -