What should be the allowed modifiers for enums and enum cases? What about:
private, protected) and annotationsRelated: #5034
Can we do implicit case in enums? This would make Shapeless-style recursive implicits much easier.
@ctongfei could you elaborate on that a little? What shapeless idiom are you referring to and how do you see implicit cases mapping on to that?
@milessabin I once created this Shapless-style thing:
trait IndexOf[A, U] extends DepFn0 {
type Out <: Nat
def toInt: Int
}
object IndexOf {
def apply[A, U](implicit o: IndexOf[A, U]): Aux[A, U, o.Out] = o
type Aux[A, U, I <: Nat] = IndexOf[A, U] { type Out = I }
implicit def case0[At <: HList, U]: Aux[U :: At, U, _0] =
new IndexOf[U :: At, U] {
type Out = _0
def apply() = Nat._0
def toInt = 0
}
implicit def caseN[At <: HList, Ah, U, I <: Nat]
(implicit p: IndexOf.Aux[At, U, I]): Aux[Ah :: At, U, Succ[I]] =
new IndexOf[Ah :: At, U] {
type Out = Succ[I]
def apply() = Succ[I]()
def toInt = p.toInt + 1
}
}
Using implicit cases, I hope that this could be simplified to something like this:
enum IndexOf[A, U] extends DepFn0 {
type Out <: Nat
def toInt: Int
implicit case Case0[At <: HList, U] extends IndexOf[U :: At, U] {
type Out = _0
def apply() = Nat._0
def toInt = 0
}
implicit case CaseN[At <: HList, Ah, U, I <: Nat]
(implicit p: IndexOf.Aux[At, U, I]) extends IndexOf[Ah :: At, U] {
type Out = Succ[I]
def apply() = Succ[I]()
def toInt = p.toInt + 1
}
}
I see, so the idea would be to use an enum as a shapeless Poly with implicit cases corresponding to the Case's of the Poly ... right?
@milessabin Yes that's my idea. You summarized it much better than I could.
@ctongfei Please open a separate issue if you think this is a use case that enums should support. This is out of scope of the current proposal for several reasons:
def. Simple cases will desugar to vals. More complex ones with type parameters and/or value parameters will desugar to classes. See http://dotty.epfl.ch/docs/reference/enums/desugarEnums.html
Most helpful comment
I see, so the idea would be to use an enum as a shapeless
Polywith implicit cases corresponding to theCase's of thePoly... right?