Jay Taylor's notes
back to listing indexWhat do <:<, <%<, and =:= mean in Scala 2.8, and where are they documented? - Stack Overflow
[web search]
I can see in the API docs for Predef that they're subclasses of a generic function type (From) => To, but that's all it says. Um, what? Maybe there's documentation somewhere, but search engines don't handle "names" like "<:<" very well, so I haven't been able to find it. Follow-up question: when should I use these funky symbols/classes, and why? | |||||||||||||||
| |||||||||||||||
These are called generalized type constraints. They allow you, from within a type-parameterized class or trait, to further constrain one of its type parameters. Here's an example:
The implicit argument Now I can use it like so:
But if I tried use it with a
You can read that error as "could not find evidence that Int == String"... that's as it should be!
This snippet by @retronym is a good explanation of how this sort of thing used to be accomplished and how generalized type constraints make it easier now. ADDENDUM To answer your follow-up question, admittedly the example I gave is pretty contrived and not obviously useful. But imagine using it to define something like a | |||||||||||||||||||||||||||||||||
|
It depends on where they are being used. Most often, when used while declaring types of implicit parameters, they are classes. They can be objects too in rare instances. Finally, they can be operators on They are meant to provide a way to test the relationship between the classes, just like As for the question "when should I use them?", the answer is you shouldn't, unless you know you should. :-) EDIT: Ok, ok, here are some examples from the library. On
On
You'll find some other examples on the collections. | |||||||||||||||||||||
|
Not a complete answer (others have already answered this), I just wanted to note the following, which maybe helps to understand the syntax better: The way you normally use these "operators", as for example in pelotom's example:
makes use of Scala's alternative infix syntax for type operators. So,
like this:
It's similar to the two syntaxes for method calls, the "normal" with | |||
add comment |
Read the other answers to understand what these constructs are. Here is when you should use them. You use them when you need to constrain a method for specific types only. Here is an example. Suppose you want to define a homogeneous Pair, like this:
Now you want to add a method
That only works if
But that seems a shame--there could be uses for the class when
It's ok to instantiate, say, a In the case of
| ||||
add comment |