Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Scala type bound error: not found: type <%<

So I've been trying to learn Scala through the twitter Scala school. But I'm currently stuck at one of their type bound examples.

In particular, it's the one where the type is bound to be viewable as a certain type, using the <%< type-relation operator.

When I execute the following code in my Scala console:

scala> class Container[A](value: A) { def addIt(implicit evidence: A <%< Int) = 123 + value }

... I get the following errors:

<console>:7: error: not found: type <%<
       class Container[A](value: A) { def addIt(implicit evidence: A <%< Int) = 123 + value }
                                                                     ^
<console>:7: error: overloaded method value + with alternatives:
  (x: Double)Double <and>
  (x: Float)Float <and>
  (x: Long)Long <and>
  (x: Int)Int <and>
  (x: Char)Int <and>
  (x: Short)Int <and>
  (x: Byte)Int <and>
  (x: String)String
 cannot be applied to (A)
       class Container[A](value: A) { def addIt(implicit evidence: A <%< Int) = 123 + value }

My question is, why is the Scala interpreter complaining?

I've been trying to look through the Scala documentation but I haven't been able to find information that operator anywhere. I can see that the Scala school was created on the basis of Scala 2.8.0, and I'm running Scala 2.10.0 - so maybe this have been removed? If this is the case, why is this as it seems like a useful operator?

like image 493
jpihl Avatar asked Feb 20 '13 15:02

jpihl


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 [+ A in Scala?

It declares the class to be covariant in its generic parameter.

What is type keyword in Scala?

Actually the type keyword in Scala can do much more than just aliasing a complicated type to a shorter name. It introduces type members. As you know, a class can have field members and method members. Well, Scala also allows a class to have type members.

What are type parameters 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.


1 Answers

The constraint A <%< B in Scala 2.8 is defined as

  sealed abstract class <%<[-From, +To] extends (From => To)
  object <%< {
    implicit def conformsOrViewsAs[A <% B, B]: A <%< B = new (A <%< B) {def apply(x: A) = x}
  }

So you can always bring it back that way. However, I'm guessing that the reason it's deprecated is that a view bound is just asking for an implicit function from A to B, and there's a perfectly good way to express that particular constraint in a generalised way already:

class Container[A](value: A) { def addIt(implicit evidence: A => Int) = 123 + value }

As an aside, it's worth noting that is isn't an operator, but a class in infix position as you can see from the definition. The same is true for =>, which is just another way of referencing the Function1 type constructor.

like image 171
Impredicative Avatar answered Sep 23 '22 09:09

Impredicative