What Scala annotations modify the compiler's messages?
I know about two:
@deprecated("use blabla instead")
is used to add an explanation to the warning output by the compiler when the annotated definition is used i开发者_如何学运维n client code.@implicitNotFound(msg = "more meaningful explanation")
is used to output an additional error message whenever an implicit of the type of the annotated definition cannot be found. Looking atCanBuildFrom
, msg can contain placeholders of the type${A}
ifA
is the name of a type parameter of the annotated type, which is filled in by the compiler with the actual expected type, e.g.:@implicitNotFound(msg = "Cannot construct a collection of type ${To} with elements of type ${Elem} based on a collection of type ${To}.") trait CanBuildFrom[-From, -Elem, +To] { ... }
Are there any other such annotations?
There is @migration
, which is used with -Xmigration
to indicate semantic changes in methods from one version to another, in helping port code between versions.
@migration(2, 8, "As of 2.8, keys returns Iterable[A] rather than Iterator[A].")
There is @tailrec, which makes the compiler output an error if tail call optimization cannot be applied to the annotated method.
As of Scala 2.9, there's also @deprecatedName
: “An annotation that designates the name of the parameter to which it is applied as deprecated. Using that name in a named argument generates a deprecation warning.”
精彩评论