When I am reading Scala reflection tutorial. I found a syntax very wired as follows.
import scala.reflect.runtime.universe._
typeOf[List[_]].member("map": TermName)
So the member
function takes Name
type parameter, and then "map": TermName
is passed into it. What does this syntax exactly mean? I am guessing it is sugar shortcut for .member(TermName("map"))
.
This syntax is called type ascription:
It is used here because the signature of
member
isso it is expecting input of type
Name
hencegives error because
"map"
is notName
. Providing type ascription"map": Name
triggers implicit conversionhowever the same can be achieved with more explicit
The implicit conversion
stringToTermName
technique is deprecated