Dotty: Allowed modifiers for enums and enum cases

Created on 22 Nov 2018  路  7Comments  路  Source: lampepfl/dotty

What should be the allowed modifiers for enums and enum cases? What about:

  • enums: access modifiers (private, protected) and annotations
  • enum cases: annotations
bug

Most helpful comment

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?

All 7 comments

Related: #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:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

felixmulder picture felixmulder  路  120Comments

odersky picture odersky  路  27Comments

letalvoj picture letalvoj  路  27Comments

japgolly picture japgolly  路  55Comments

neko-kai picture neko-kai  路  39Comments