Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a scala generic class in java

I have this in scala:

object Tester {
  def apply[T : Manifest](description: String, initValue: T) = new Tester[T](description, initValue)
  implicit def TesterToValue[T](j: Tester[T]): T = j.value
}

class Tester[T : Manifest](someData: String, initValue: T) {
  private var value = initValue
  def getValue : T = value
}

which allows me to do

val t1 = JmxValue("some data", 4)

I'm trying to create an instance of this is java, so far I've had no luck I've tried:

Tester t1 = Tester<Integer>("data", 0);
Tester<Integer> t1 = Tester<Integer>("data", 0);
Tester t1 = Tester("data", 0);
Tester t1 = new Tester<Integer>("data", 0);
Tester<Integer> t1 = new Tester<Integer>("data", 0);

Is there some limitation in using scala generic classes in java? Or am I just doing something horribly wrong

like image 376
James Lee Avatar asked Feb 13 '14 22:02

James Lee


People also ask

How do I use generics in Scala?

Type-Safety With Generic ClassesWhen declaring a class in Scala, we can specify type parameters. We use square brackets to surround these type parameters. For example, we can declare class Foo[A]. The placeholder A can then be used in the body of the class to refer to the type.

How do you define a generic class in Java?

A Generic class simply means that the items or functions in that class can be generalized with the parameter(example T) to specify that we can add any type as a parameter in place of T like Integer, Character, String, Double or any other user-defined type.

How do you invoke a generic method?

To call a generic method, you need to provide types that will be used during the method invocation. Those types can be passed as an instance of NType objects initialized with particular . NET types.

What is the use of generic methods and generic classes in Java?

Java Generic methods and generic classes enable programmers to specify, with a single method declaration, a set of related methods, or with a single class declaration, a set of related types, respectively. Generics also provide compile-time type safety that allows programmers to catch invalid types at compile time.


1 Answers

Your Tester class actually has an implicit parameter (because of the [T : Manifest] type boundary. The syntax you use is sugar for

// Scala Class
class Tester[T](someData: String, initValue: T)(implicit man: Manifest[T]){...}

When that gets compiled, the two argument lists are condensed to one, so you end up with the java equivalent of

//Java Constructor
public Tester(String someData, T initValue, Manifest<T> man){...}

You can see the type signature by running the javap command on the Tester.class file that gets generated by the scala compiler.

So if you are trying to use the Tester class from Java, you have to explicitly figure out the parameter that scala's compiler would normally figure out for you.

Looking at the scaladocs, it looks like ManifestFactory is where you need to go to create Manifest instances. So your java code would look something like

Manifest<Integer> man = ManifestFactory$.MODULE$.classType(Integer.class);
Tester<Integer> tester = new Tester<Integer>("data", 123, man);
like image 168
Dylan Avatar answered Sep 29 '22 20:09

Dylan