Reputation: 251
I'm trying to convert the following Java code into Scala:
ImageRecognitionPlugin imageRecognition = (ImageRecognitionPlugin)nnet.getPlugin(ImageRecognitionPlugin.class)
It runs in Java (full code: http://neuroph.sourceforge.net/image_recognition.html).
What would it be in Scala? I'm confused about the "(ImageRecognitionPlugin).nnet
" bit and I get the following error when I drop the "(ImageRecognitionPlugin)
".
"error: object ImageRecognitionPlugin is not a value"
Upvotes: 0
Views: 318
Reputation: 7257
Try this:
val imageRecognition:ImageRecognitionPlugin =
nnet.getPlugin(classOf[ImageRecognitionPlugin])
Let's break this down:
val
This declares an immutable value. That means that this value will always point to this specific instance. If you used a var instead this would be a variable instead.
imageRecognition:ImageRecognitionPlugin
This tells us 1) the name of the value and 2) its type. Instead of Foo aFoo, as in Java, Scala uses aFoo:Foo.
nnet.getPlugin(classOf[ImageRecognitionPlugin])
classOf[Foo] is Scala's equivalent of Foo.class in Java.
Upvotes: 1
Reputation: 51369
In scala you need to specify whether you are create a mutable (var) or immutable (val) variable. You also need to use classOf and instanceOf instead of .class and the (cast):
val imageRecognition = nnet.getPlugin(classOf[ImageRecognitionPlugin]).asInstanceOf[ImageRecognitionPlugin]
Upvotes: 4