【发布时间】:2019-09-15 12:04:58
【问题描述】:
我有一个“vertex”(平面中的 2D 点,用包含两个双精度值的地图表示)和一个“rectangle”(一个平面中轴对齐的矩形区域,由两个“vertex”值的映射表示),定义如下:
; vertex coordinates are doubles (need to make sure "=" doesn't try to
; compare floats in one vertex and doubles in another one, which fails)
(s/def ::vtx-x double?)
(s/def ::vtx-y double?)
; vertex is a map representing a 2D point
(s/def ::vertex (s/keys :req [::vtx-x ::vtx-y]))
; rectangle corners "low" (low x,y) and "high" (high x,y) are vertexes
(s/def ::rec-lo ::vertex)
(s/def ::rec-hi ::vertex)
; rectangle has internal sorting constraint, specified via a predicate
(s/def ::rectangle-internally-sorted
(fn [rect]
(let [lo-vtx (::rec-lo rect)
hi-vtx (::rec-hi rect)
lo-x (::vtx-x lo-vtx)
lo-y (::vtx-y lo-vtx)
hi-x (::vtx-x hi-vtx)
hi-y (::vtx-y hi-vtx)]
(< lo-x hi-x) (< lo-y hi-y))))
; rectangle is a map of two vertexes
; representing an axis-aligned rectangular area
(s/def ::rectangle
(s/and
(s/keys :req [::rec-lo ::rec-hi])
::rectangle-internally-sorted))
客户端代码应该只能使用规范 ::vertex 和 ::rectangle。
我可以隐藏(私有化)支持规范吗?
【问题讨论】:
标签: clojure clojure.spec