Knows Not Much
Knows Not Much

Reputation: 31526

Scala. Making a class generic

I have this code which appears over and over in my project for each type of class we serialize to Avro

class FooKryoRegistrator extends KryoRegistrator {
  override def registerClasses(kryo: Kryo) {
    kryo.register(classOf[Foo], SpecificRecordBinarySerializer(classTag[Foo]))    
    kryo.register(classOf[GenericData.Array[_]], new SpecificInstanceCollectionSerializer(classOf[java.util.ArrayList[_]]))
  }
}

Now instead of having to write this Registrator class for each and every entity (Foo, Bar, Baz.....) in my project, I thought it will be better to make this generic. So I did

class GenericKryoRegistrator[T] extends KryoRegistrator {
  override def registerClasses(kryo: Kryo) {
    kryo.register(classOf[T], SpecificRecordBinarySerializer(classTag[T]))    
    kryo.register(classOf[GenericData.Array[_]], new SpecificInstanceCollectionSerializer(classOf[java.util.ArrayList[_]]))
  }
}

But I get a compile time error

class type required but T found

I googled on this error and found a recommendation that I should use ClassTag instead of class. So I changed the implementation to

class GenericKryoRegistrator[T] extends KryoRegistrator {
  override def registerClasses(kryo: Kryo) {
    kryo.register(classTag[T].runtimeClass, SpecificRecordBinarySerializer(classTag[T]))    
    kryo.register(classOf[GenericData.Array[_]], new SpecificInstanceCollectionSerializer(classOf[java.util.ArrayList[_]]))
  }
}

but now it says

No ClassTag available for T

Upvotes: 0

Views: 103

Answers (1)

Angelo Genovese
Angelo Genovese

Reputation: 3398

If you take the class tag as an implicit argument to your class, you can then use it:

class GenericKryoRegistrator[T] (implicit ct: ClassTag[T]) extends KryoRegistrator {
  override def registerClasses(kryo: Kryo) {
    kryo.register(ct.runtimeClass, SpecificRecordBinarySerializer(ct))
    kryo.register(classOf[GenericData.Array[_]], new SpecificInstanceCollectionSerializer(classOf[java.util.ArrayList[_]]))
  }
}

Upvotes: 2

Related Questions