Skip to content

Instantly share code, notes, and snippets.

@jorgeortiz85
Created September 21, 2011 16:29
Show Gist options
  • Save jorgeortiz85/1232550 to your computer and use it in GitHub Desktop.
Save jorgeortiz85/1232550 to your computer and use it in GitHub Desktop.
attempt at newtype in Scala for FKs
class FK[A] extends StaticAnnotation
class User
class Checkin
object Main {
def fetch[A](id: Int @ FK[A]): A = null.asInstanceOf[A]
def main(args: Array[String]): Unit = {
val id: Int @ FK[Checkin] = 10
// Why does this type check?
fetch[User](id)
}
}
@retronym
Copy link

Miles' approach might have obviated the need for annotation checking, but I think you can add extra typechecking with a custom AnnotationChecker.

AnnotationCheckers:

/** Register an annotation checker.  Typically these
   *  are added by compiler plugins. */
  def addAnnotationChecker(checker: AnnotationChecker) {
    if (!(annotationCheckers contains checker))
      annotationCheckers = checker :: annotationCheckers
  }

Typers:662

  • (-1) For expressions with annotated types, let AnnotationCheckers decide what to do

Types:4458

tp1 match {
      case _: AnnotatedType =>
        return annotationsConform(tp1, tp2) && annotationsConform(tp2, tp1) && tp1.withoutAnnotations =:= tp2.withoutAnnotations
      case _ =>
    }

@jorgeortiz85
Copy link
Author

Pretty hot, thanks! Miles' approach looks promising, but if it doesn't work out I'll give this a shot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment