macro classConversions[From, To] extends Annotation with StaticAnnotation
Annotate a trait with To] this to generate a bunch of handy implicits for using one type
(From) interchangeably with another (To) in tests:
Annotate a trait with To] this to generate a bunch of handy implicits for using one type
(From) interchangeably with another (To) in tests:
\@Conversions[Int, String]
trait IntToStringConversions {
// Implicitly convert integers to strings by concatenating their string representations to themselves.
implicit def intToString(n: Int): String = s"$n$n"
}
The annotation will unroll a bunch of implicit functions for converting from e.g. Option[Int] to
Option[String], Seq[Int] to Seq[String], etc. If an implicit => String is not give in the body of
the trait, then one needs to be in scope where it is declared.
Downstream classes can then mix-in IntToStringConversions to have all such implicits in scope.
A macro is used so that multiple different Conversions traits can be mixed-in to a given concrete class without
the implicit-names colliding (which would otherwise result in all but the last-to-be-mixed-in being hidden/shadowed);
instead, the macro seeds all implicits' names with the types being converted between, avoiding the issue.
In order to use the Conversions annotation, macro paradise must be enabled.
Annotate a trait with To] this to generate a bunch of handy implicits for using one type (From) interchangeably with another (To) in tests:
\@Conversions[Int, String] trait IntToStringConversions { // Implicitly convert integers to strings by concatenating their string representations to themselves. implicit def intToString(n: Int): String = s"$n$n" }
The annotation will unroll a bunch of implicit functions for converting from e.g. Option[Int] to Option[String], Seq[Int] to Seq[String], etc. If an implicit => String is not give in the body of the trait, then one needs to be in scope where it is declared.
Downstream classes can then mix-in
IntToStringConversions
to have all such implicits in scope.A macro is used so that multiple different Conversions traits can be mixed-in to a given concrete class without the implicit-names colliding (which would otherwise result in all but the last-to-be-mixed-in being hidden/shadowed); instead, the macro seeds all implicits' names with the types being converted between, avoiding the issue.
In order to use the Conversions annotation, macro paradise must be enabled.