LoginSignup
5
5

More than 5 years have passed since last update.

開発時ではないときに assertion を止める

Last updated at Posted at 2015-11-01

assert を使って事前条件と事後条件を表明するという契約プログラミング的なスタイルは Clojure においても実践可能でそれは次のように書くことが出来ます。

(defn foo [x]
  {:pre [(map? x)]}
  x)

(foo 1) ;; AssertionError Assert failed: (map? x) 
(foo {:bar 10}) ;; {:bar 10}

とはいえ、これらは開発時に使いたいものであってプロダクション環境などでは動いてほしくないものです。
なのでこれを止めるようにします。

(defproject assert-test "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.7.0"]]
  :profiles
  {:dev {:global-vars {*assert* true}}
   :prod {:global-vars {*assert* false}}})

:global-vars を設定するだけで良いです。例えば lein uberjar するときに行いたいなら :uberjar プロファイルに設定するなどでも良いと思います。

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5