In a simple way, what are context and view bounds and what is the difference between them?
Some easy-to-follow examples would be great too!
In a simple way, what are context and view bounds and what is the difference between them?
Some easy-to-follow examples would be great too!
I thought this was asked already, but, if so, the question isn't apparent in the "related" bar. So, here it is:
What is a View Bound?
A view bound was a mechanism introduced in Scala to enable the use of some type
A
as if it were some typeB
. The typical syntax is this:In other words,
A
should have an implicit conversion toB
available, so that one can callB
methods on an object of typeA
. The most common usage of view bounds in the standard library (before Scala 2.8.0, anyway), is withOrdered
, like this:Because one can convert
A
into anOrdered[A]
, and becauseOrdered[A]
defines the method<(other: A): Boolean
, I can use the expressiona < b
.Please be aware that view bounds are deprecated, you should avoid them.
What is a Context Bound?
Context bounds were introduced in Scala 2.8.0, and are typically used with the so-called type class pattern, a pattern of code that emulates the functionality provided by Haskell type classes, though in a more verbose manner.
While a view bound can be used with simple types (for example,
A <% String
), a context bound requires a parameterized type, such asOrdered[A]
above, but unlikeString
.A context bound describes an implicit value, instead of view bound's implicit conversion. It is used to declare that for some type
A
, there is an implicit value of typeB[A]
available. The syntax goes like this:This is more confusing than the view bound because it is not immediately clear how to use it. The common example of usage in Scala is this:
An
Array
initialization on a parameterized type requires aClassManifest
to be available, for arcane reasons related to type erasure and the non-erasure nature of arrays.Another very common example in the library is a bit more complex:
Here,
implicitly
is used to retrive the implicit value we want, one of typeOrdering[A]
, which class defines the methodcompare(a: A, b: A): Int
.We'll see another way of doing this below.
How are View Bounds and Context Bounds implemented?
It shouldn't be surprising that both view bounds and context bounds are implemented with implicit parameters, given their definition. Actually, the syntax I showed are syntactic sugars for what really happens. See below how they de-sugar:
So, naturally, one can write them in their full syntax, which is specially useful for context bounds:
What are View Bounds used for?
View bounds are used mostly to take advantage of the pimp my library pattern, through which one "adds" methods to an existing class, in situations where you want to return the original type somehow. If you do not need to return that type in any way, then you do not need a view bound.
The classic example of view bound usage is handling
Ordered
. Note thatInt
is notOrdered
, for example, though there is an implicit conversion. The example previously given needs a view bound because it returns the non-converted type:This example won't work without view bounds. However, if I were to return another type, then I don't need a view bound anymore:
The conversion here (if needed) happens before I pass the parameter to
f
, sof
doesn't need to know about it.Besides
Ordered
, the most common usage from the library is handlingString
andArray
, which are Java classes, like they were Scala collections. For example:If one tried to do this without view bounds, the return type of a
String
would be aWrappedString
(Scala 2.8), and similarly forArray
.The same thing happens even if the type is only used as a type parameter of the return type:
What are Context Bounds used for?
Context bounds are mainly used in what has become known as typeclass pattern, as a reference to Haskell's type classes. Basically, this pattern implements an alternative to inheritance by making functionality available through a sort of implicit adapter pattern.
The classic example is Scala 2.8's
Ordering
, which replacedOrdered
throughout Scala's library. The usage is:Though you'll usually see that written like this:
Which take advantage of some implicit conversions inside
Ordering
that enable the traditional operator style. Another example in Scala 2.8 is theNumeric
:A more complex example is the new collection usage of
CanBuildFrom
, but there's already a very long answer about that, so I'll avoid it here. And, as mentioned before, there's theClassManifest
usage, which is required to initialize new arrays without concrete types.The context bound with the typeclass pattern is much more likely to be used by your own classes, as they enable separation of concerns, whereas view bounds can be avoided in your own code by good design (it is used mostly to get around someone else's design).
Though it has been possible for a long time, the use of context bounds has really taken off in 2010, and is now found to some degree in most of Scala's most important libraries and frameworks. The most extreme example of its usage, though, is the Scalaz library, which brings a lot of the power of Haskell to Scala. I recommend reading up on typeclass patterns to get more acquainted it all the ways in which it can be used.
EDIT
Related questions of interest: