Showing posts with label Context Bound. Show all posts
Showing posts with label Context Bound. Show all posts

Sunday, 3 June 2012

Scala: Type Classes

Type classes were originally developed in Haskell as a disciplined alternative to ad-hoc polymorphism. Type classes have been shown to provide a type-safe solution to important challenges in software engineering and programming languages such as, for example, retroactive extension of programs. They are also recognized as a good mechanism for concept-based generic programming and, more recently, have evolved into a mechanism for type-level computation. Rephrasing in simple words: if U got conception of Context Bound in Scala U almost get type classes implementation in Scala.

Scala: View Bound and Context Bounds

View Bound


A view bound is Scala's mechanism to enable the use of some type A as if it were some type T (Real type). The typical syntax is this:

    def func[A <% T](a: A) = a.methodFromT