【问题标题】:Can't create a System protocol无法创建系统协议
【发布时间】:2016-09-26 21:17:20
【问题描述】:

我正在写Entity Component System。我计划的一部分是拥有一个名为System 的协议,系统必须满足该协议才能使用 ECS。

问题是,如果我尝试创建一个名为 System 的协议,Clojure 会报错;似乎是因为它与java.lang.System 冲突。

(ns entity.system)

(defprotocol System
  ; Protocol methods)

产量

CompilerException java.lang.RuntimeException: Expecting var, but System is mapped to class java.lang.System, 正在编译:(C:\Users\slomi\IdeaProjects\entity\src\entity\system.clj:3:1 )

我尝试通过添加(:refer-clojure :exclude [System])(:refer-clojure :exclude [java.lang.System]) 来排除System,但什么都没做;我又收到了同样的错误。

当然,我可以将其命名为其他名称,但 System 似乎是最合适的名称,而像 entity.entity-system/Entity-System 甚至 entity.system/Entity-System 这样的名称似乎过于多余。

如何从命名空间中排除java.lang.System

【问题讨论】:

    标签: clojure protocols


    【解决方案1】:

    你要找的是ns-unmap

    (ns-unmap *ns* 'System)
    
    (defprotocol System
      (add [this that]))
    
    (extend-protocol System
      Long
      (add [this that]
        (format "%d + %d is %d" this that (+ this that))))
    
    (add 2 3)
    ;;=> "2 + 3 is 5"
    

    【讨论】:

    • 可爱。谢谢。
    猜你喜欢
    • 2019-08-29
    • 1970-01-01
    • 2020-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多