clojure Java互操作之 extend-type

作者: admin 日期: 2018-06-19 18:03:14 人气: - 评论: 0

在clojure与Java的互操作中有个小问题,就是Java的对象是没有成员属性的这个概念的,在clojure可以使用extend-type这个宏为Java的对象添加成员属性


(extend-type java.util.Date
  IMatchLookup
  (val-at* [this k not-found]
    (case k      :year    (.getYear this)      :month   (.getMonth this)      :date    (.getDate this)      :hours   (.getHours this)      :minutes (.getMinutes this)
      not-found)))

(let [d (java.util.Date. 2010 10 1 12 30)]
  (match [d]
    [{:year 2009 :month a}] [:a0 a]
    [{:year (:or 2010 2011) :month b}] [:a1 b]));=> [:a1 10]


相关内容

发表评论
更多 网友评论0 条评论)
暂无评论

Copyright © 2012-2014 我的代码板 Inc. 保留所有权利。

页面耗时0.0300秒, 内存占用1.82 MB, 访问数据库13次

闽ICP备15009223号-1