Note that there are some explanatory texts on larger screens.

plurals
  1. POScala: Typed Method that returns a subType
    primarykey
    data
    text
    <p>I'm trying to write a function that returns a partially applied function which returns a subType of a particular abstract class.</p> <p>I have an abstract class </p> <pre><code>abstract class IsoBoxReader </code></pre> <p>I have a derived class </p> <pre><code>class FileTypeBoxReader( val box, val isoReader ) extends IsoBoxReader </code></pre> <p>I want to do something like this and I'm not sure if its possible:</p> <pre><code>def recognize[ T &lt;: IsoBoxReader ]( box ): (IsoReader) =&gt; T = { box.boxType match { case "ftyp" =&gt; ( isoReader: IsoReader ) =&gt; new FileTypeBoxReader( box, isoReader ) case _ =&gt; // return some other box type } } </code></pre> <p>The above method based on one piece of information returns a partially applied function which can be used at a later place where the remaining necessary argument is available.</p> <p>Some of you might have guessed that I'm trying to parse media Iso files based on the ISO Specification. I'm doing this basically for the experience and it seems like an interesting OO and functional design problem. </p> <p>To create a specific box reader I need a basic box which is the header info and a reader object which basically does the specific file reading. I want to write a function that given a box ( which has the box type ) I get a partially applied function that returns a sub-type of IsoBoxReader. This way caller can create the actual instance with its version of the IsoReader ( the file reading, stateful object ). The compiler is complaining about the pattern match, specifically calling out a Type Mismatch.</p> <p>It says found FileTypeBoxReader but expected T</p> <p>However, based on the Type argument, isn't FileTypeBoxReader a valid return value since it is a sub-type of IsoBoxReader ?</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