4

I am using java API that looks like:

import com.google.code.morphia.Morphia;

.....

     val morphia = new Morphia();
     morphia.map(Hotel.class).map(Address.class);

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

Perception
  • 79,279
  • 19
  • 185
  • 195
ace
  • 11,526
  • 39
  • 113
  • 193

3 Answers3

7

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

Hopefully this should fix your problem, although it's hard to tell without seeing the full compiler output.

prat_c
  • 276
  • 2
  • 5
  • This fixed the compiler error. Hopefully I will also not get runtime error when I complete the code. – ace Jun 26 '11 at 13:34
  • Its unfortunate that scala chose this format which requires almost twice as much typing than in standard Java. I though scala requires less typing. – ace Jun 26 '11 at 14:21
  • 1
    Scala *does* require less typing; in practice, you'll find that the `classOf[T]` construct is almost never needed. The usual technique to deal with type-dependent logic in Scala is pattern matching. – Kevin Wright Jun 26 '11 at 14:58
  • @amc Scala does not have static methods, since static methods don't make sense in an object oriented environment (what's their `this`?). So `ClassName.class` doesn't make sense -- it can't be a static method, and `ClassName` is not an object. On the other hand, `classOf[T]` is a method on object `Predef`, so it makes perfect sense. – Daniel C. Sobral Jun 27 '11 at 00:17
3

You should use classOf[Hotel]

Knut Arne Vedaa
  • 15,372
  • 11
  • 48
  • 59
1

Edit: After having looked at the API; what you actually want is:

classOf[T]

eg.

classOf[Hotel]

More info on Scala’s typesystem.


Wrong answer, but maybe useful, if someone is mislead by this questions title:

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

Hotel.`class`
Community
  • 1
  • 1
Debilski
  • 66,976
  • 12
  • 110
  • 133