KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I'd like to know how do the member types work in Scala, and how should I associate types. 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. 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) A concrete example would be: I have a trait for DFAs (could be without the type parameter) trait DFA[S] { /* S is the type of the symbols in the alphabet */ trait State { def next(x : S); } /* final type Sigma = S */ } and I want to create a function for running this DFA over an input sequence, and I want the function must take anything <% Seq[alphabet-type-of-the-dfa] as input sequence type the function caller needn't specify the type parameters, all must be inferred 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) the alphabet types must be unconstrained (ie. there must be a DFA for Char as well as for a yet unknown user-defined class) the DFAs with different alphabet types are not subtypes I tried this: def runDFA[S, D <: DFA[S], SQ <% Seq[S]](d : D)(seq : SQ) = .... this works, except the type S is not inferred here, so I have to write the whole type parameter list on each call site. def runDFA[D <: DFA[S] forSome { type S }, SQ <% Seq[D#Sigma]]( ... same as above this
Tags (comma-separated)
Save Edits
Cancel