Is it possible to create circular references in Clojure?

前端 未结 2 861
你的背包
你的背包 2021-02-12 10:06

Ignoring native interop and transients, is it possible to create any data structures in Clojure that contain direct circular references ?

It would seem that immutable da

2条回答
  •  孤城傲影
    2021-02-12 10:50

    In Clojure most circular data structures will explicitely go through a ref type of some kind (eg atom).

    However you can create a circular sequence (it's somewhat an oxymoron):

    (let [a (atom nil)] (reset! a (lazy-seq (cons 1 @a))))
    

    And since Clojure 1.2 with deftype you can create other datatypes which can introduce circularity without using explicitely (from the usercode at least) any kind of ref type.

提交回复
热议问题