milessabin / shapeless

Generic programming for Scala

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add support for type parameters in annotations

pgrandjean opened this issue · comments

Follow up on issue #922 and pull request #925. I would like to add support for type parameters in annotations, both variable and type annotations. For example, define an annotation such as:

case class MyAnnotation[T](t: T) extends scala.annotation.StaticAnnotation

The out type of Annotations and TypeAnnotations would have the complete "reference" to type T.

Moreover, what about generalizing Annotations to return all annotations instead of just one annotation type A? Example:

case class HelloWorld(
  field1: String @A11[T111, T112, ..., T11n] @A12[T121, T122, ..., T12n] ... @A1m[T1m1, T1m2, ..., T1mn],
  field2: String @A21[T211, T212, ..., T21n] @A22[T221, T222, ..., T22n] ... @A2m[T2m1, T2m2, ..., T2mn],
  ...,
  fieldk: String @Ak1[Tk11, Tk12, ..., Tk1n] @Ak2[Tk21, Tk22, ..., Tk2n] ... @Akm[Tkm1, Tkm2, ..., Tkmn],
)

Would be nice to have a single macro to extract in one pass all annotations, no?

The trait would look like the following:

trait AllAnnotations[T] extends DepFn0 with Serializable {
  // no more type A
  type Out <: HList
}

For the example above, the Out type would be an HList of HLists:

(A11[T111, T112, ..., T11n] :: A12[T121, T122, ..., T12n] :: ... :: A1m[T1m1, T1m2, ..., T1mn] :: HNil) ::
(A21[T211, T212, ..., T21n] :: A22[T221, T222, ..., T22n] :: ... :: A2m[T2m1, T2m2, ..., T2mn] :: HNil) ::
... ::
(Ak1[Tk11, Tk12, ..., Tk1n] :: Ak2[Tk21, Tk22, ..., Tk2n] :: ... :: Akm[Tkm1, Tkm2, ..., Tkmn] :: HNil) :: HNil

AFAIC, I think it would be nice to have.

I have a working prototype. Will try to submit a PR in the coming days.

@joroKr21 just wondering, is this going to work with Scala 3?

@joroKr21 just wondering, is this going to work with Scala 3?

The Scala 3 story is a bit messy for shapeless because it uses a lot of macros.

This particular implementation won't work on Scala 3.
There is Shapeless 3 which works only with Scala 3 on the shapeless-3 branch.
I don't know much about it yet, so I can't say what it offers in terms of annotations.
Separately from that we have an issue to port shapeless 2 in some form to Scala 3: #1043
But nobody is working on that at the moment.

At least there is a step in that direction: #1019
Next would be, write a scalafix rule to replace Lazy and Strict with by-name implicits in the source code.