9

I am reading Programming Scala right now. I just got through the chapter on implicit type conversion, where the <% symbol is introduced. There is also a <: symbol and a < symbol.

Could someone please summarize the different type constraints? I am struggling with the difference between <: and < for instance. I am curious if there are any others I haven't covered yet.

Travis Parks
  • 8,435
  • 12
  • 52
  • 85
  • Possible duplicate of http://stackoverflow.com/questions/4465948/what-are-scala-context-and-view-bounds see also http://stackoverflow.com/questions/3427345/what-do-and-mean-in-scala-2-8-and-where-are-they-documented – om-nom-nom Apr 01 '13 at 16:53
  • @om-nom-nom I am not concerned with the operators addressed in the post you listed. I concerned the type constraint operators: `<:`, `<%`, `>:`, etc. They are completely unrelated. – Travis Parks Apr 02 '13 at 19:56

1 Answers1

17

There is no type constraint called <.

A <: B means A is literally a subtype of B (where subtype is defined reflexively, meaning for any type T it is the case that T <: T).

A <% B means A is either a subtype of B or there is an implicit conversion from A to a distinct type AA for which AA <: B. This is called a "view bound."

A >: B means A is supertype of B.

Randall Schulz
  • 26,420
  • 4
  • 61
  • 81
  • You're right, there is no `<` constraint. What about the `>:` constraint? – Travis Parks Apr 01 '13 at 17:50
  • I just started reading the chapter on implementing `List`. It explains that `>:` can be used to created a `List[Fruit]` when adding an `Orange` to a `List[Apple]`. It's a very interesting constraint. – Travis Parks Apr 02 '13 at 19:58
  • More precisely: A <% B means there is an implicit conversion from A to a distinct type AA for which AA <: B. If A is a subtype of B, AA may be the same as A: The identity function (which is defined in Predef and always in scope) can be applied instead of an actual conversion. But even if A is a subtype of B, there may be an implicit conversion to a different type AA that takes precedence over Predef.identity(). In that case, the compiler would infer type AA. Depends on precendence of implicits. – jcsahnwaldt Reinstate Monica Mar 24 '15 at 14:58