Note that there are some explanatory texts on larger screens.

plurals
  1. POAvoiding explicit casts in Factory
    primarykey
    data
    text
    <p>I have an external DSL (Combinator-Parsers) to define <code>Extractor[A]</code> where A is the type that is extracted when the Extractor has been executed.</p> <p>An extractor consists of steps that call the next step as the continuation. The extractor creates intermediate extractions (string-based key/value-pairs), which are finally transformed into the proper <code>A</code>s by a factory.</p> <p>Extractor: <code>case class Extractor[A](id: String, start: Step[A], factory: Factory[A])</code></p> <p>Step: <code>sealed trait Step[A] { def process(ctx: Context[A]): List[A] }</code></p> <p>Factory: <code>trait Factory[A] { def create(extracts: List[(String,String)]): List[A] }</code></p> <p>Currently my extractor-parser looks like the following:</p> <pre><code> val extractor: Parser[Extractor[_]] = trace ~ "EXTRACT" ~ extractorType ~ extractorId ~ step ^^ { case t ~ _ ~ ty ~ id ~ step =&gt; { val extr = ty match { case "SUBTITLES" =&gt; { Extractor[MovieInfos.Subtitle](id, step.asInstanceOf[Step[MovieInfos.Subtitle]], new SubtitleFactory) } case x =&gt; throw new InvalidExtractorType(x) } extr.trace(t) } } </code></pre> <p><strong>EDIT</strong>: The step-parser has signature <code>val step: Parser[Step[_]]</code> (the step-parser is not able to know that type of <code>Step[A]</code> it is exactly parsing. Finally the step gets its proper <code>A</code>-type by the extractor-parser's <code>asInstanceOf</code>)</p> <p>The above extractor parser is currently only able to parse extractors of extractorType <code>"SUBTITLES"</code>. I want to add other extractorTypes successively, e.g. <code>case "REVIEWS" =&gt; ...</code>.</p> <p>For each extractor <code>Extractor[A]</code>, I have to provide a specialized factory (e.g. <code>new SubtitleFactory</code>) that creates things of type <code>A</code> (e.g. <code>MovieInfos.Subtitle</code>) and pass matching <code>step</code>s into the extractor (that's what the <code>asInstanceOf</code> is for).</p> <p>The parser has to parse different Extractor-Types (Subtitles, Images, Audiotracks, etc.), so I use <code>Parser[Extractor[_]]</code>, is this properly typed or is there any better way?</p> <p>Furthermore, I want to avoid that <code>step.asInstanceOf[Step[MovieInfos.Subtitle]]</code> which looks a bit fishy since I already provided the proper type <code>Extractor[MovieInfos.Subtitle]</code></p> <p>And finally I want a more flexible Factory, possibly extensible by some sort of configuration/hooks. </p> <p>Any recommendations appreciated, thanks.</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