0

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

1 Answer 1

2

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[_]]))
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. how will the caller instantiate this class then? how will we get the actual parameter for the implicit?
I'm not sure of the mechanics, but I believe the compiler will provide the classtag 'automagically'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.