开发者

How to overload "get" in Clojure

开发者 https://www.devze.com 2023-02-17 19:49 出处:网络
I wish to use the clojure \"get\" keyword for my own function. How can I prevent clojure from using the \"get\" defi开发者_JAVA技巧ned in the standard libraries?As mentioned this is not necessarily a

I wish to use the clojure "get" keyword for my own function. How can I prevent clojure from using the "get" defi开发者_JAVA技巧ned in the standard libraries?


As mentioned this is not necessarily a good idea, but you can do it like this:

user=> (ns your.name.space
  (:refer-clojure :exclude [get]))
nil
your.name.space=> (defn get [] "something")
#'your.name.space/get
your.name.space=> 


I wouldn't recommend using get for anything but getting a value out of a collection, since that's what anyone reading your code would expect it to do.

If you don't want to do that, Wodin's answer is what you want.

If you actually want to "overload" get as per your title, that is, make the standard get function work with your own collection type - just make sure your collection implements ILookup, Map, or IPersistentSet and you can provide your own get/valAt method.

0

精彩评论

暂无评论...
验证码 换一张
取 消