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

Using Android Volley to post an array to PHP -

python - How to add an model instance to a django queryset? -

angularjs - No 'Access-Control-Allow-Origin' error from local domain to public API -