scala - Extending Enum in scalaz and implicit parameters with subtypes -
this question has answer here:
- typeclasses , inheritance in scalaz 1 answer
i playing scalaz , thought extend enum type class make myself understand scalaz better. wrote this:
sealed abstract trait counter case object first extends counter case object second extends counter case object third extends counter implicit val enumcounter: enum[counter] = new enum[counter] { override def succ(a: counter): counter = match { case first => second case second => third case third => first } override def pred(a: counter): counter = match { case first => third case second => first case third => second } override def order(x: counter, y: counter): ordering = { val map = map[counter, int](first -> 0, second -> 1, third -> 2) implicitly[order[int]].order(map(x), map(y)) } } println(first |=> third) println(first.succ) but turns out doesn't work hoped will. first not have succ nor |=>, because i've created enum[counter] not enum[first]. if write first.asinstanceof[counter].succ starts resolve. obvious me. how can implement enum typeclass in simple way? not want declare separate implicit values each of enum[first], enum[second]....
there 2 possible solutions thinking of:
1) make scala resolve enum[first] enum[counter]. cannot seem understand how can possible enum can nonvariant
2) maybe there solution in scalaz?
otherwise enum typeclass starts quite limited, not supports enum sounds weird.
i not sure how question belongs scalaz, depends on whether solution (1) or (2). if solution (1) - question pure scala.
i rethought question , rephrased lot - , received answer, please see this: typeclasses , inheritance in scalaz
Comments
Post a Comment