使用deftype实现具有类型参数的通用接口

huangapple go评论49阅读模式
英文:

Implementing generic interface with type parameters using deftype

问题

是否可以将类型参数传递给由deftype实现的泛型接口?我试图在一个依赖类型参数的框架中工作,但似乎无法在纯Clojure中这样做。我可以退回到纯Java,但出于其他原因,这会使代码和构建配置变得更加繁琐,所以理想情况下我想保持它纯粹。

Java中我要编写的代码的等价物如下所示:

public class BlahBlahSerializer implements SerializationCustomSerializer<BlahBlah, BlahBlahProxy> {
  /* implementation here */
}

我正在寻找一种在Clojure中编写类似以下代码的方式:

(deftype BlahBlahSerializer []
  ^{:generic-type-parameters [BlahBlah BlahBlahProxy]} SerializationCustomSerializer
  /* implementation here */)

这种做法是否可能?

英文:

Is it possible to pass a type parameter into a generic interface that is being implemented by deftype? I'm trying to work with a framework that relies heaviliy on type parameters, and got stuck with apparent inability to do so with pure Clojure. I could fall back to pure Java, but it makes code and build configuration more cumbersome for other reasons, so ideally I would like to keep it pure.

Java equvalent of the code I'm looking to write is like the following:

public class BlahBlahSerializer implements SerializationCustomSerializer&lt;BlahBlah, BlahBlahProxy&gt; {
  /* implementation here */
}

I'm looking for a way to write something like this in Clojure:

(deftype BlahBlahSerializer []
  ^{:generic-type-parameters [BlahBlah BlahBlahProxy]} SerializationCustomSerializer
  /* implementation here */)

Is it possible at all?

答案1

得分: 0

我在为Apache Beam编写Clojure库时遇到了这个问题,其中Beam使用泛型的反射进行各种用途,但无法找到解决方法。我的结论是,使用Clojure无法将此信息传递到生成的类中,尽管这并不是最终结论。我的解决方案是编写小的Java类,定义了这些内容,但其方法实现立即委托给Clojure代码。

https://github.com/zendesk/clj-headlights

英文:

I ran into this issue when writing a clojure library for Apache Beam, where Beam used reflection of the generics for various purposes, and was unable to find a way around it. My conclusion was that there's no way to get this information into the emitted class using clojure, though that's not definitive. My solution was to write small classes that defined those things in Java but whose method implementations immediately delegated to Clojure code.

https://github.com/zendesk/clj-headlights

huangapple
  • 本文由 发表于 2020年1月4日 01:37:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582939.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定