Summoning an instance by means of a method context parameter allows to customize the error message declaratively annotating the parameter with @implicitNotFound which requires a literal string that supports a type placeholder.
import scala.annotation.implicitNotFound
inline def singleInhabitantOf[A](using @implicitNotFound("The received type ${A} is not a singleton") voA: ValueOf[A]): A = voA.value
singleInhabitantOf[Option[Int]] // Compile error: The received type Option[Int] is not a singleton
If instead the summoning is implemented with summonFrom
, we can customize the error message imperatively calling the error
method which also requires a literal string but has no type placeholder support.
import scala.compiletime.{summonFrom, error}
inline def singleInhabitantOf2[A]: A = summonFrom {
case voA: ValueOf[A] => voA.value
case _ => error("The received type ${A} is not a singleton")
}
singleInhabitantOf2[Option[Int]] // compiler error: The received type ${A} is not a singleton
Is there a way, other than using macros, to summon an implicit instance in the body (like summonFrom
does) that has type placeholder support or, much better, dynamic string interpolation?