2014-02-27 3 views

답변

1

당신은 Dire 대신

(ns mytask 
    (:require [dire.core :refer [with-precondition! with-handler!]])) 

(defn add-one [n] 
    (inc n)) 

(with-precondition! #'add-one 
    "An optional docstring." 
    ;;; Name of the precondition 
    :not-two 
    (fn [n & args] 
    (not= n 2))) 

(with-handler! #'add-one 
    {:precondition :not-two} 
    (fn [e & args] (apply str "Precondition failure for argument list: " (vector args)))) 

(add-one 2) ; => "Precondition failure for argument list: (2)" 
1

전제 조건이 충족되어야합니다 조건이나 다른 예외가 발생합니다 있습니다 사용할 수 있습니다. 거짓 일 때 예외가 throw되는 조건이있는 경우 complement 조건부 또는 결과 : not입니다.

user=> (defn magic? [n] (= 0 (rem n 42))) 
#'user/magic? 

user=> (defn foo [n] {:pre [(magic? n)]} n) 
#'user/foo 
user=> (foo 42) 
42 

user=> (defn bar [n] {:pre [(not (magic? n))]} n) 
#'user/bar 

user=> (bar 42) 
AssertionError Assert failed: (not (magic? n)) user/bar 

user=> (defn baz [n] {:pre [((complement magic?) n)]} n) 
#'user/baz 
user=> (baz 42) 

AssertionError Assert failed: ((complement magic?) n) user/baz 
관련 문제