- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个仅发出事件的 java 接口(interface),我正在尝试在 Clojure 中实现它。 Java接口(interface)是这样的(现实中还有很多其他方法):
public interface EWrapper {
void accountSummary(int reqId, String account, String tag, String value, String currency);
void accountSummaryEnd(int reqId);
}
我的 Clojure 代码如下所示:
(defn create
"Creates a wrapper calling a single function (cb) with maps that all have a :type to indicate
what type of messages was received, and event parameters
"
[cb]
(reify
EWrapper
(accountSummary [this reqId account tag value currency]
(dispatch-message cb {:type :account-summary :request-id reqId :account account :tag tag :value value :currency currency}))
(accountSummaryEnd [this reqId]
(dispatch-message cb {:type :account-summary-end :request-id reqId}))
))
我有大约 75 个函数要“实现”,我的所有实现所做的就是调度一个看起来像 {:type waiting-function-name-kebab-case :parameter-one-kebab-caseparameter-one- 的映射value :parameter-two-kebab-caseparameter-two-value}
等。对于另一个宏来说似乎已经成熟了 - 这也会更安全,因为底层接口(interface)会更新更多功能,我的实现也会如此。
这可能吗?我该如何开始呢?我的理想情况是直接读取 .java 代码,但我也可以手动将 Java 代码粘贴到映射结构中?谢谢,
最佳答案
你可以自己解析出简单的方法数据(我自己没有尝试过反射API)。这是一个示例,包括要演示的单元测试。
首先,将 Java 源代码放入 Clojure 数据结构中:
(ns tst.demo.core
(:use tupelo.core tupelo.test)
(:require
[camel-snake-kebab.core :as csk]
[schema.core :as s]
[tupelo.string :as ts]))
(def java-spec
(quote {:interface EWrapper
:methods [; assume have structure of
; <ret-type> <method-name> <arglist>, where <arglist> => (<type1> <name1>, <type2> <name2> ...)
void accountSummary (int reqId, String accountName, String tag, String value, String currencyName)
void accountSummaryEnd (int reqId)
]
}))
然后,一个函数将方法规范分开,并将参数解构为类型和名称。我们使用一个库将 CamelCase 转换为 kabob-case:
(defn reify-gen
[spec-map]
(let [methods-data (partition 3 (grab :methods spec-map))
; >> (spyx-pretty methods-data)
method-entries (forv [mdata methods-data]
(let [[ret-type mname arglist] mdata ; ret-type unused
mname-kebab (csk/->kebab-case mname)
arg-pairs (partition 2 arglist)
arg-types (mapv first arg-pairs) ; unused
arg-names (mapv second arg-pairs)
arg-names-kebab (mapv csk/->kebab-case arg-names)
arg-names-kebab-kw (mapv ->kw arg-names-kebab)
mresult (list mname (prepend
(quote this)
arg-names)
(list
mname-kebab
(glue {:type (->kw mname-kebab)}
(zipmap arg-names-kebab-kw arg-names))))]
; (spyx-pretty mresult)
mresult ))]
(->list
(prepend
(quote reify)
(grab :interface spec-map)
method-entries))))
以及一个单元测试来演示:
(dotest
(newline)
(is= (spyx-pretty (reify-gen java-spec))
(quote
(reify
EWrapper
(accountSummary
[this reqId accountName tag value currencyName]
(account-summary
{:type :account-summary
:req-id reqId,
:account-name accountName,
:tag tag,
:value value,
:currency-name currencyName}))
(accountSummaryEnd
[this reqId]
(account-summary-end {:type :account-summary-end, :req-id reqId})))
))
)
关于java - Clojure reify - 使用另一个宏自动实现 Java 接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61281406/
下面的测试类,通过。 class SimpleClassTest { private inline fun anyObject(): T { return Mockito.a
我需要获取 lambda 函数实现的 AST。我被卡住了,因为 reify 对论点起作用,而不是对它的值(value)起作用。 val x = (a: Int) => println("a") val
基于 this comment关于具体化, It acts like @property, except that the function is only ever called once; aft
我有一些这样的代码: {-# LANGUAGE AllowAmbiguousTypes #-} module Foo where import Data.Proxy class Foo x y cla
当使用reify时在 Clojure 中,如何为构造函数提供表达式? 或者,如何将参数传递给基类构造函数? 最佳答案 您不能使用reify来子类化和实例化类——它仅适用于协议(protocol)和接口
我尝试阅读论文( http://www.ittc.ku.edu/csdl/fpg/sites/default/files/Gill-09-TypeSafeReification.pdf )并设法具体化
是否有可能做到 reify在 GHCi 中? 当我使用“runQ”尝试它时,它提示“无法在 IO monad 中进行具体化”。 >>> runQ (reify ''Bool) Template Has
为什么我们需要Control.Lens.Reified ?是否有某种原因导致我无法将 Lens 直接放入容器中? reify 到底是什么意思? 最佳答案 我们需要具体化的镜头,因为 Haskell 的
我正在尝试围绕如下所示的 Java API 编写 Clojure 层: public class Executor { public interface ExecutorJob { pub
我在 Pyramid tutorial for UX design 中看到了.我无法弄清楚这个装饰器到底是什么。 我看到其用法的示例代码。 def __init__(self, request):
我试图理解 reified 关键字的用途,显然是 it's allowing us to do reflection on generics . 但是,当我将其省略时,它也可以正常工作。任何人愿意解释
我一直在使用 Scala 宏,并在宏中包含以下代码: val fieldMemberType = fieldMember.typeSignatureIn(objectType) match {
为什么我应该在 clojure 中使用 Reify 而不是代理? 最佳答案 reify 的方法体是词法闭包,可以引用周围的局部作用域。 reify 与 proxy 的不同之处在于: 仅支持协议(pro
我编写了一些代码,以使用“DefineTypeGeneric”类检测类型模型数据。 主类 fun main() { val list = ArrayList() list.add("K
实际上,主要问题仍然是 Kotlin 中的类没有具体化的类型参数。但这就是为什么在这种特定情况下这困扰我的原因: 假设您有一个包装类 Wrapper接受一个字符串 content和一个类(class)
我在有关 haskell 和函数式编程的博客中读了很多这个术语(特别是 sigfpe's blog ),但我不知道它的含义。大多数时候我都不知道,但如果我知道的话,我可能会更好地理解这些文本。谷歌没有
我有一个仅发出事件的 java 接口(interface),我正在尝试在 Clojure 中实现它。 Java接口(interface)是这样的(现实中还有很多其他方法): public interf
我正在尝试编写一个断言函数来检查给定对象是否属于 T 类型: @UseExperimental(ExperimentalContracts::class) inline fun assertIsIn
我真的可以用你的帮助来解释这个功能。 我有一个包含 2 个字符串值、一个国家名称和一个货币的国家枚举。练习是使用 2 个字符串输入,检查枚举是否有国家,然后比较它们的货币,相应地返回 true 或 f
我正在寻找 -print 或 javap 的替代方案来了解编译器在 Scala 中执行的操作。有了新的反射/宏库,reify 似乎是一个很好的候选者,如 Retronym 的 macrocosm 所示
我是一名优秀的程序员,十分优秀!