我有,我想创建基于作为参数连同地图的属性来记录实例的类型记录的新实例的情况下。创建同一类型的记录作为另一个
(defn record-from-instance
[other attrs]
;; Code that creates the new record based on "other"
)
我现在所拥有的是在电线之间的东西:
(defn record-from-instance
[other attrs]
(let [matched (s/split (subs (str (class other)) 6) #"\.")
path (s/join "." (pop matched))
class-name (peek matched)]
((resolve (symbol (str path "/" "map->" class-name))) attrs)))
是否有其他更简单更地道的方式来做到这一点,我不能看?
谢谢!
编辑
给一些更多的细节,我建设有节点被记录和我使用的是拉链参观,并可能更改/删除AST的部分的AST。我有一个IZipableTreeNode
协议
(defprotocol IZipableTreeNode
(branch? [node])
(children [node])
(make-node [node children]))
实现了不同类型之间的IZipableTreeNode
是IPersistentMap
IPersistentMap
(branch? [node] true)
(children [node] (seq node))
(make-node [node children]
(let [hmap (into {} (filter #(= (count %) 2)) children)]
(if (record? node)
(record/from-instance node hmap)
hmap)))
当访问者说,从节点删除字段(或改变吧)make-node
被调用node
成为创纪录的AST节点和children
新的键/值对(可能不包含node
中的某些字段)。
真的这整个问题和你对它的所有评论让我问:“你为什么使用记录?”这听起来像是你希望记录的表现完全像地图一样,并且你要完成所有这些工作来绕过它们的核心功能。 – amalloy
@amalloy是我使用记录作为AST的节点。你会不会更喜欢用带有_type_字段的简单地图呢? – g7s
在说明中添加了一些更多详细信息 – g7s