In specs2 there is a method called Around, documented here that has the following example:
object http extends Around {
def around[T <% Result](t: =>T) = openHttpSession("test") {
t // execute t inside a http session
}
}
The source for this code can be found here.
I'm curious what the <% operator means in this context?
EDIT: here is a solid answer on this subject, What are Scala context and view bounds?
=> is syntactic sugar for creating instances of functions. Recall that every function in scala is an instance of a class. For example, the type Int => String , is equivalent to the type Function1[Int,String] i.e. a function that takes an argument of type Int and returns a String .
Methods in Scala can be parameterized by type as well as by value. The syntax is similar to that of generic classes. Type parameters are enclosed in square brackets, while value parameters are enclosed in parentheses.
Most Scala generic classes are collections, such as the immutable List, Queue, Set, Map, or their mutable equivalents, and Stack. Collections are containers of zero or more objects. We also have generic containers that aren't so obvious at first.
This is a view bound. It means, that the type T
must be convertible to the type Result
. For more information about type bounds I recommend you http://www.cs.uwaterloo.ca/~brecht/courses/702/Possible-Readings/scala/ProgrammingInScala.pdf, starting at page 61.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With