1
clojure의 pre-expr은 멋지지만, :pre
이 false 일 때 예외를 발생 시키려면 가능합니까?clojure pre-expr에서 예외를 발생시키는 방법은 무엇입니까?
감사합니다.
clojure의 pre-expr은 멋지지만, :pre
이 false 일 때 예외를 발생 시키려면 가능합니까?clojure pre-expr에서 예외를 발생시키는 방법은 무엇입니까?
감사합니다.
당신은 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)"
전제 조건이 충족되어야합니다 조건이나 다른 예외가 발생합니다 있습니다 사용할 수 있습니다. 거짓 일 때 예외가 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