How to specify Foo.class in scala?

I am using a java API that looks like this:

import com.google.code.morphia.Morphia; ..... val morphia = new Morphia(); morphia.map(Hotel.class).map(Address.class); 

but it gives a scala compiler error. What is the correct code in scala for above? Please note that .map is defined as part of the morphia API and not to be confused with the scala map.

+4
source share
3 answers

Assuming your problem is with parts of .class , the equivalent of Scala Hotel.class would be classOf[Hotel] , similarly for Address .

Hope this should solve your problem, although it's hard to say without seeing the full compiler output.

+7
source

You must use classOf[Hotel]

+3
source

Edit : by looking at the API; what do you really want:

 classOf[T] 

eg.

 classOf[Hotel] 

Additional information in the Scala systemystem.


The answer is incorrect, but possibly useful if someone is misleading in the name of the questions:

class is a reserved word in Scala. If you want to use it as the name of a field or method, you need to enclose it with reverse windows:

 Hotel.`class` 
+1
source

All Articles