Renaming classOf to Scala

I am working on a readable DSL user for ScalaTest. At the moment I can write

feature("Admin Login") { scenario("Correct username and password") { given("user visits", classOf[AdminHomePage]) then(classOf[SignInPage], "is displayed") 

but it will be much better to read how

 feature("Admin Login") { scenario("Correct username and password") { given("user visits", the[AdminHomePage]) then(the[SignInPage], "is displayed") 

Is there any way

 def the[T] = 

to return classOf[T] ?

+7
source share
2 answers

What you probably want to do is simply rename the method (which is defined in the Predef object) when importing:

 import Predef.{ classOf => the, _ } 

Note that classOf will no longer work if you rename it like this. If you still need to add this import:

 import Predef.classOf; 

For more information on renaming, see also:

+3
source

You can try the following:

 def the[T: ClassManifest]: Class[T] = classManifest[T].erasure.asInstanceOf[Class[T]] 

The designation [T: ClassManifest] is a context binding and is equivalent to:

 def the[T](implicit classManifest: ClassManifest[T]) 

Implicit values โ€‹โ€‹for Manifest[T] and ClassManifest[T] automatically populated by the compiler (if it can confirm the type parameter passed to the method) and provide you with information about the T runtime: ClassManifest just ClassManifest it as Class[_] and Manifest can additionally tell to you about the possible parameterization of T itself (for example, if T is Option[String] , then you can also learn about the String part).

+17
source

All Articles