Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the <% operator mean in Scala generics? [duplicate]

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?

like image 542
Jeff Wu Avatar asked Jul 17 '12 04:07

Jeff Wu


People also ask

What is the meaning of => in Scala?

=> 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 .

What is square brackets in Scala?

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.

What are generics Scala?

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.


1 Answers

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.

like image 108
tgr Avatar answered Sep 22 '22 12:09

tgr