From 1bab9b74f0c89c503af2d2a23edd629d92623e55 Mon Sep 17 00:00:00 2001 From: Peter Taoussanis Date: Fri, 8 Sep 2023 15:25:48 +0200 Subject: [PATCH] [nop] Misc housekeeping --- src/taoensso/encore.cljc | 134 +++++++++++++++++++-------------------- 1 file changed, 66 insertions(+), 68 deletions(-) diff --git a/src/taoensso/encore.cljc b/src/taoensso/encore.cljc index c558f1be..1447bb73 100644 --- a/src/taoensso/encore.cljc +++ b/src/taoensso/encore.cljc @@ -133,6 +133,38 @@ (remove-ns 'taoensso.encore) (test/run-tests)) +;;;; + +(defn ^:no-doc -unexpected-arg! + "Private low-level util." + {:added "v3.66.0 (2023-08-23)"} + ([arg] (-unexpected-arg! arg nil)) + ([arg {:keys [msg] :as details}] + (throw + (ex-info (or msg (str "Unexpected argument: " arg)) + (conj {:arg {:value arg, :type (type arg)}} details))))) + +(defn unexpected-arg! + "Throws runtime `ExceptionInfo` to indicate an unexpected argument. + Takes optional kvs for merging into exception's data map. + + (let [mode :unexpected] + (case mode + :read (do <...>) + :write (do <...>) + (unexpected-arg! mode + :expected #{:read :write}))) => + + Unexpected argument: :unexpected + {:arg {:value :unexpected, :type clojure.lang.Keyword}, + :expected #{:read :write}}" + + {:added "v3.51.0 (2023-03-13)"} + [arg & {:keys [msg] :as details}] + (-unexpected-arg! arg details)) + +(comment (unexpected-arg! :arg :expected '#{string?})) + ;;;; Core macros #?(:clj @@ -140,36 +172,32 @@ "Evaluates `test`. If it returns logical true (and doesn't throw), expands to `then`, otherwise expands to `else`." {:style/indent 1} - ([test then ] `(compile-if ~test ~then nil)) ; Back compatibility + ([test then ] `(compile-if ~test ~then nil)) ([test then else] (if (try (eval test) (catch Throwable _ false)) `(do ~then) `(do ~else))))) +#?(:clj (defmacro compile-when {:style/indent 1} [test & body] `(compile-if ~test (do ~@body) nil))) + #?(:clj (defmacro try-eval - "Evaluates `form`. If eval doesn't throw, expands to `form`, otherwise to nil." + "If `form` can be successfully evaluated at macro-expansion time, expands to `form`. + Otherwise expands to `nil`." {:added "v3.50.0 (2023-03-07)"} [form] `(compile-if ~form ~form nil))) -(comment - (macroexpand '(try-eval (java.util.concurrent.atomic.AtomicLong.))) - (macroexpand '(try-eval (java.util.concurrent.Executors/newVirtualThreadPerTaskExecutor))) - (macroexpand '(try-eval (do (println "effect") :x)))) +(comment (macroexpand '(try-eval (com.google.common.io.BaseEncoding/base16)))) -#?(:clj (defmacro compile-when {:style/indent 1} [test & body] `(compile-if ~test (do ~@body) nil))) #?(:clj - (compile-if - ;; Avoiding for edge case where user cares about startup time and has - ;; `core.async` as dependency but it never gets required anywhere else - ;; (do (require 'clojure.core.async) true) - (do (or - (io/resource "clojure/core/async.clj") - (io/resource "clojure/core/async.cljc"))) - (def have-core-async? true) - (def have-core-async? false))) - -(comment (require '[clojure.core.async] :verbose)) + (def have-core-async? + "Is `core.async` available?" + (compile-if + (boolean + (or + (io/resource "clojure/core/async.cljc") + (io/resource "clojure/core/async.clj"))) + false))) #?(:clj (defmacro if-let @@ -441,14 +469,6 @@ `(cljs.core/defonce ~sym ~@body) `(clojure.core/defonce ~sym ~@body))))) -;;;; Core fns - -(def ^:private core-merge #?(:clj clojure.core/merge :cljs cljs.core/merge)) -(def ^:private core-update-in #?(:clj clojure.core/update-in :cljs cljs.core/update-in)) -(declare merge update-in) - -;;;; Secondary macros - #?(:clj (defmacro identical-kw? "Returns true iff two keywords are identical. @@ -472,12 +492,6 @@ ~@(map-indexed (fn [i# form#] (if (even? i#) (eval form#) form#)) clauses) ~(when default default))))) -#?(:clj - (do - (defmacro do-nil [& body] `(do ~@body nil)) - (defmacro do-false [& body] `(do ~@body false)) - (defmacro do-true [& body] `(do ~@body true)))) - #?(:clj (defmacro doto-cond "Cross between `doto`, `cond->` and `as->`." @@ -491,6 +505,20 @@ ~@(map pstep (partition 2 clauses)) ~g)))) +#?(:clj + (do + (defmacro do-nil [& body] `(do ~@body nil)) + (defmacro do-false [& body] `(do ~@body false)) + (defmacro do-true [& body] `(do ~@body true)))) + +;;;; + +(def ^:private core-merge #?(:clj clojure.core/merge :cljs cljs.core/merge)) +(def ^:private core-update-in #?(:clj clojure.core/update-in :cljs cljs.core/update-in)) +(declare merge update-in) + +;;;; + #?(:clj (defmacro declare-remote "Declares given ns-qualified symbols, preserving metadata. Useful for @@ -580,8 +608,6 @@ (when ~link? (-alias-link-var (var ~alias-sym) ~src-var ~alias-attrs)) (do (var ~alias-sym))))))))) -(declare -unexpected-arg!) - #?(:clj (defmacro defaliases "Bulk version of `defalias`. @@ -622,9 +648,11 @@ "Private, used by other Taoensso libs." {:added "v3.57.0 (2023-03-29)"} [& types] - `(do ~@(map (fn [type] - `(defmethod print-method ~type [~'x ~(with-meta 'w {:tag 'java.io.Writer})] - (.write ~'w (str ~(str "#" *ns* ".") ~'x)))) types)))) + `(do + ~@(map + (fn [type] + `(defmethod print-method ~type [~'x ~(with-meta 'w {:tag 'java.io.Writer})] + (.write ~'w (str ~(str "#" *ns* ".") ~'x)))) types)))) (comment (do @@ -1438,36 +1466,6 @@ ;;;; -(defn ^:no-doc -unexpected-arg! - "Private low-level util." - {:added "v3.66.0 (2023-08-23)"} - ([arg] (-unexpected-arg! arg nil)) - ([arg {:keys [msg] :as details}] - (throw - (ex-info (or msg (str "Unexpected argument: " arg)) - (conj {:arg {:value arg, :type (type arg)}} details))))) - -(defn unexpected-arg! - "Throws runtime `ExceptionInfo` to indicate an unexpected argument. - Takes optional kvs for merging into exception's data map. - - (let [mode :unexpected] - (case mode - :read (do <...>) - :write (do <...>) - (unexpected-arg! mode - :expected #{:read :write}))) => - - Unexpected argument: :unexpected - {:arg {:value :unexpected, :type clojure.lang.Keyword}, - :expected #{:read :write}}" - - {:added "v3.51.0 (2023-03-13)"} - [arg & {:keys [msg] :as details}] - (-unexpected-arg! arg details)) - -(comment (unexpected-arg! :arg :expected '#{string?})) - (defn instance! "If (instance? class arg) is true, returns arg. Otherwise throws runtime `ExceptionInfo` with `unexpected-arg!`. @@ -5672,7 +5670,7 @@ ((:fn (tf-state (after-timeout 200 (println *foo*) *foo*)))))) ;;;; DEPRECATED -;; {:deprecated "v ()" :doc "Prefer ``"} +;; {:deprecated "v ()" :doc "Prefer ``."} #?(:clj (defmacro deprecated