nativelibs4java / scalaxy-evidence

Macros for magic evidence typeclasses: HasAnnotation, negative evidences...

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build Status

Simple typeclasses that check that a given annotation is set (or not) on a given class.

For instance, take javax.persistence.Entity: it is set on classes that are JPA entities. A sensible persist[T] method should not accept types T which do not have an @Entity annotation. Also, one may not want to persist entities marked with the @Deprecated annotation (which is not retained at runtime).

This is where this library can help you:

  import javax.persistence.Entity
  import scalaxy.evidence._

  type IsEntity[T] = HasAnnotation[T, Entity]

  type IsNotDeprecated[T] = ![HasAnnotation[T, Deprecated]]

  def persist[T : IsEntity : IsNotDeprecated](t: T) = ???

These contracts are simple: persist[T] requires the static proof that T is annotated with @Entity and not with @Deprecated.

Now if you take the following code:

@Entity(name = "...")
class GoodEntity

@Entity(name = "...") @Deprecated
class DeprecatedEntity

class NotAnEntity

serialize(new GoodEntity)        // Ok.
serialize(new DeprecatedEntity)  // Error: failed to prove IsNotDeprecated
serialize(new NotAnEntity)       // Error: failed to prove IsEntity

About

Macros for magic evidence typeclasses: HasAnnotation, negative evidences...

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:Scala 100.0%