Note that there are some explanatory texts on larger screens.

plurals
  1. POScala type inference with generics and covariance, possible issue in scalac
    primarykey
    data
    text
    <p>I have a short list of questions that I've found today. I'm a scala novice, therefore my questions may be trivial.</p> <p>Let's assume we have a class like this one:</p> <pre><code>abstract class A[+T] { def foo[S &gt;: T](x: S): String } </code></pre> <p>And we should provide useful child class with some functionality.</p> <p>1) My first attempt looks like this:</p> <pre><code>class B extends A[String] { def foo(x: String) = x } </code></pre> <p>but scalac compiler disagrees with message:</p> <pre><code>xxx@yyy:~$ scalac covariant.scala covariant.scala:3: error: class B needs to be abstract, since method foo in class A of type [S &gt;: String](x: S)String is not defined class B extends A[String] { def foo(x: String) = x } ^ </code></pre> <p>First of all, why scalac didn't infer foo's generic type parameter, is it really a complex task?</p> <p>2) Next try looks better and should be accepted, as I think:</p> <pre><code>class B extends A[String] { def foo[String](x: String) = x } </code></pre> <p>but now compiler made my eyes wide open:</p> <pre><code>covariant.scala:3: error: type mismatch; found : String(in method foo) required: java.lang.String class B extends A[String] { def foo[String](x: String) = x } ^ </code></pre> <p>It looks like an issue with that mismatch of String and java.lang.String. This is a second question: Is it really a bug?</p> <p>3) Finally I've decided to switch String type to Date as parameter:</p> <pre><code>import java.util.Date class B extends A[Date] { def foo[Date](x: Date) = x.toString } </code></pre> <p>It have been compiled without any warnings. So last question is: Why? What is the difference between my second and third snippets?</p> <p>By the way, scalac version is <code>2.9.1.final</code></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload