Skip to content

Allowed modifiers for enums and enum cases #5495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
allanrenucci opened this issue Nov 22, 2018 · 7 comments
Closed

Allowed modifiers for enums and enum cases #5495

allanrenucci opened this issue Nov 22, 2018 · 7 comments
Assignees

Comments

@allanrenucci
Copy link
Contributor

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

  • enums: access modifiers (private, protected) and annotations
  • enum cases: annotations
@allanrenucci
Copy link
Contributor Author

Related: #5034

@ctongfei
Copy link

Can we do implicit case in enums? This would make Shapeless-style recursive implicits much easier.

@milessabin
Copy link
Contributor

@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?

@ctongfei
Copy link

@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
    }

}

@milessabin
Copy link
Contributor

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?

allanrenucci added a commit to dotty-staging/dotty that referenced this issue Nov 27, 2018
allanrenucci added a commit to dotty-staging/dotty that referenced this issue Nov 27, 2018
allanrenucci added a commit to dotty-staging/dotty that referenced this issue Nov 27, 2018
@ctongfei
Copy link

@milessabin Yes that's my idea. You summarized it much better than I could.

@allanrenucci
Copy link
Contributor Author

allanrenucci commented Nov 27, 2018

@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:

@odersky odersky closed this as completed in a037261 Jan 1, 2020
odersky added a commit that referenced this issue Jan 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants