Note that there are some explanatory texts on larger screens.

plurals
  1. POType parameters versus member types in Scala
    text
    copied!<p>I'd like to know how do the member types work in Scala, and how should I associate types.</p> <p>One approach is to make the associated type a type parameter. The advantages of this approach is that I can prescribe the variance of the type, and I can be sure that a subtype doesn't change the type. The disadvantages are, that I cannot infer the type parameter from the type in a function.</p> <p>The second approach is to make the associated type a member of the second type, which has the problem that I can't prescribe bounds on the subtypes' associated types and therefore, I can't use the type in function parameters (when x : X, X#T might not be in any relation with x.T)</p> <p>A concrete example would be:</p> <p>I have a trait for DFAs (could be without the type parameter)</p> <pre><code>trait DFA[S] { /* S is the type of the symbols in the alphabet */ trait State { def next(x : S); } /* final type Sigma = S */ } </code></pre> <p>and I want to create a function for running this DFA over an input sequence, and I want</p> <ul> <li>the function must take anything <code>&lt;% Seq[alphabet-type-of-the-dfa]</code> as input sequence type</li> <li>the function caller needn't specify the type parameters, all must be inferred</li> <li>I'd like the function to be called with the concrete DFA type (but if there is a solution where the function would not have a type parameter for the DFA, it's OK)</li> <li>the alphabet types must be unconstrained (ie. there must be a DFA for Char as well as for a yet unknown user-defined class)</li> <li>the DFAs with different alphabet types are not subtypes</li> </ul> <p>I tried this:</p> <pre><code>def runDFA[S, D &lt;: DFA[S], SQ &lt;% Seq[S]](d : D)(seq : SQ) = .... </code></pre> <p>this works, except the type S is not inferred here, so I have to write the whole type parameter list on each call site.</p> <pre><code>def runDFA[D &lt;: DFA[S] forSome { type S }, SQ &lt;% Seq[D#Sigma]]( ... same as above </code></pre> <p>this didn't work (invalid circular reference to type D??? (what is it?))</p> <p>I also deleted the type parameter, created an abstract type Sigma and tried binding that type in the concrete classes. runDFA would look like</p> <pre><code>def runDFA[D &lt;: DFA, SQ &lt;% Seq[D#Sigma]]( ... same as above </code></pre> <p>but this inevitably runs into problems like "type mismatch: expected <code>dfa.Sigma</code>, got <code>D#Sigma</code>"</p> <p>Any ideas? Pointers?</p> <p>Edit:</p> <p>As the answers indicate there is no simple way of doing this, could somebody elaborate more on why is it impossible and what would have to be changed so it worked?</p> <p>The reasons I want runDFA ro be a free function (not a method) is that I want other similar functions, like automaton minimization, regular language operations, NFA-to-DFA conversions, language factorization etc. and having all of this inside one class is just against almost any principle of OO design.</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