Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a scala object in clojure

I'm trying to create a scala object, kafka.utils.ZKStringSerializer in clojure. (It's in org.apache.kafka/kafka_2.10 "0.8.0")

Since I know little about scala, I don't know how to call its constructor. I tried like this:

(import 'kafka.utils.ZKStringSerializer)
(ZKStringSerializer.)                      
; or (new ZKStringSerializer)

And got an error: CompilerException java.lang.IllegalArgumentException: No matching ctor found for class kafka.utils.ZKStringSerializer

I tried using (clojure.reflect/reflect ZKStringSerializer) to see its methods but there are only some static methods. And (class ZKStringSerializer) tells me it is a class, not an instance I want.

The object is implemented like this:

object ZKStringSerializer extends ZkSerializer {

  @throws(classOf[ZkMarshallingError])
  def serialize(data : Object) : Array[Byte] = data.asInstanceOf[String].getBytes("UTF-8")

  @throws(classOf[ZkMarshallingError])
  def deserialize(bytes : Array[Byte]) : Object = {
    if (bytes == null)
      null
    else
      new String(bytes, "UTF-8")
  }
}
like image 653
halfelf Avatar asked Dec 20 '22 18:12

halfelf


1 Answers

All scala objects are singletons in terms of java. There is no public constructor. You should use static field MODULE$ to get an instance of singleton.

I don't know clojure, but according to this page it looks like you should use this:

ZKStringSerializer$/MODULE$

Note also that the actual type name of object contains $ as last character.

like image 87
senia Avatar answered Jan 07 '23 19:01

senia