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
memberisso it is expecting input of type
Namehencegives error because
"map"is notName. Providing type ascription"map": Nametriggers implicit conversionhowever the same can be achieved with more explicit
The implicit conversion
stringToTermNametechnique is deprecated