I have automata, which have dependent types (I would call them associated) for the states and labels of their transitions.
trait Automaton {
type State
type Label
type Transition = (State, Label, State)
}
Now I want to write a class that takes as its argument an automaton and a function doing some sort of counting for transitions on that particular automaton. I would write:
class AutomataMagic(val aut: Automaton, val h: aut.Transition => Int) {
...
}
However, that doesn't compile. Is there any way I can specify that I want my function to do things to specifically this automaton's transition type?
The error message says it all:
As Luis Miguel Mejía Suárez said, you need to move
h
to another parameter group.Oddly enough, that doesn't work (probably because of this bug).
A (very naive) workaround would be this. It's not very satisfying, but it seems to work.
EDIT: Luis Miguel Mejía Suárez suggested using a trait with a companion object. Here's my interpretation of that:
By the way, the first snippet compiles in Scala 3.
Also, you really shouldn't use this:
Not only is
A#Transition
unrelated toaut.Transition
(at least make itaut: A
), type projections are unsound and will be dropped in Scala 3. Maybe it won't matter to you in this specific case becauseTransition
is invariant, but it is not safe.