Note that there are some explanatory texts on larger screens.

plurals
  1. POInheritance and (automatic?) type conversion
    text
    copied!<p>Please have a look at the followin code, where <code>Extractor[A,B]</code> is part of a generic framework and everything else should be regarded as "client code" (I boiled it a down quite a bit and renamed everything. So don't mind that <code>Extractor</code> doesn't seem to be too usefull).</p> <pre><code>scala&gt; abstract class Extractor[A,B] { | def extract(d:A):B | def stringRepr(d:A):String | } defined class Extractor scala&gt; sealed abstract class Value defined class Value scala&gt; case class IntValue(i:Int) extends Value defined class IntValue scala&gt; case class StringValue(s:String) extends Value defined class StringValue scala&gt; case class Data(i:Int, s:String) defined class Data scala&gt; sealed abstract class MyExtractor[Value] extends Extractor[Data, Value] { | def stringRepr(d:Data) = extract(d) match { | case IntValue(i) =&gt; i.toString | case StringValue(s) =&gt; s | } | } defined class MyExtractor scala&gt; class IntExtractor(name:String) extends MyExtractor[IntValue] { | def extract(d:Data) = IntValue(d.i) | } defined class IntExtractor scala&gt; class StringExtractor(name:String) extends MyExtractor[StringValue] { | def extract(d:Data) = StringValue(d.s) | } defined class StringExtractor </code></pre> <p>so in short words <code>Extractor[A,B]</code> is used to extract some value <code>B</code> from <code>A</code> and do some other things that are not represented in this show code. The abstract classes <code>Value</code> and <code>MyExtractor</code> are used for reasons of type savety in the "client code". When I try to create a <code>List</code> of <code>MyExtractor</code>s, the following happens:</p> <pre><code>scala&gt; val l = List.empty[MyExtractor[Value]] l: List[MyExtractor[Value]] = List() scala&gt; new IntExtractor("test1") :: l res5: List[MyExtractor[_ &gt;: IntValue &lt;: Value]] = List(IntExtractor@1fd96c5) </code></pre> <p>trying to convert an <code>IntExractor</code> to a superclass</p> <pre><code>scala&gt; new IntExtractor("test"):MyExtractor[Value] &lt;console&gt;:24: error: type mismatch; found : IntExtractor required: MyExtractor[Value] new IntExtractor("test"):MyExtractor[Value] ^ scala&gt; new IntExtractor("test"):Extractor[Data,Value] &lt;console&gt;:24: error: type mismatch; found : IntExtractor required: Extractor[Data,Value] new IntExtractor("test"):Extractor[Data,Value] </code></pre> <p>I am aware that everything is fine, when I define <code>IntExtractor</code> like this</p> <pre><code>scala&gt; class IntExtractor(name:String) extends MyExtractor[Value] { | def extract(d:Data) = IntValue(d.i) | } defined class IntExtractor scala&gt; new IntExtractor("test"):Extractor[Data,Value] res17: Extractor[Data,Value] = IntExtractor@1653d7a </code></pre> <p>But I don't understand, why it doesn't work the way I tried it above. I would be thankfull for any help or hints.</p>
 

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