Reading the Scala by Example book and there is this example when Martin explains type bounds on page 54:
trait Set[A <: Ordered[A]] {
def incl(x: A): Set[A]
def contains(x: A): Boolean
}
and
trait Set[A <% Ordered[A]] ...
further on page 55. He also says that the <:/<% is the only change required for trait Set in order to demonstrate the type bounding possibilities.
However, when I repeat the example with my own code, the IDE complains that traits may NOT have view bounds, only type bounds. Changing the trait keyword to abstract class or changing the view bound to type bound helps. Is this a mistake in the book?
Let's use our powerful tool called the REPL to understand what is going on:
scala> trait Example[A<:Ordered[A]] { def contains(x:A):Boolean }
defined trait Example
scala> class Example2[A<%Ordered[A]]( val a:A) { def isLower(otherA:A):Boolean = a< otherA }
defined class Example2
scala> :javap Example
Compiled from "<console>"
public interface Example{
public abstract boolean contains(scala.math.Ordered);
}
scala> :javap Example2
Compiled from "<console>"
public class Example2 extends java.lang.Object implements scala.ScalaObject{
public java.lang.Object a();
public boolean isLower(java.lang.Object);
public Example2(java.lang.Object, scala.Function1);
}
As you can see , the view bound becomes the second argument of the Example2 constructor. Since a trait does not have a constructor, clearly it is not possible to provide a view bound.
Why this has been possible in previous releases is to me a mistery (maybe an additional Function1 val was created inside the trait and filled by the compiler?)
Concerning your question about Scala evolution, it is mature and powerful. You can expect changes between major releases (2.8, 2.9, 2.10) but I would not consider scala not mature enough for this. There is, however, always space for improvement
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