Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
darkleaf committed Jul 4, 2024
1 parent 547391a commit 66e373b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
47 changes: 44 additions & 3 deletions src/darkleaf/di/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -568,13 +568,51 @@
(defn- var->0-service [variable]
variable)



(comment

(let [f (with-meta
(fn
([] 0)
([a b c d] 4)
([a b c d & _] :n))
{:foo :bar})]
[(f)
(f 1 2 3 4)
(f 1 2 3 4 5)])



,,,)


(defn- var->service [variable]
(let [deps (dependencies-fn variable)]
(let [deps (dependencies-fn variable)
name (str "#di/service[" variable "]")]
;; => #di/service[#'darkleaf.di.service-name-test/service]
(reify p/Factory
(dependencies [_]
deps)
deps)
(build [_ deps]
(partial variable deps))
(-> variable
(partial deps)

;; with-meta медленнее partial, т.к. фактически делает (apply f args)
;; https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/AFunction.java#L31
;; и все равно в сообщении об ошибке будет имя класса, а не print-method

;; Можно сделать свой (deftype Service [variable deps] IFn ...)
;; defrecord тут нельзя
;; Так в ошибках будет
;; class darkleaf.di.service.Service cannot be cast to class
;; вместо
;; partial: class clojure.core$partial$fn__5908 cannot be cast to class
;; with-meta: class clojure.lang.AFunction$1 cannot be cast to class

(with-meta
{:type ::service
:name name})))
(demolish [_ _]))))

(defn- var->factory-defn [variable]
Expand Down Expand Up @@ -635,6 +673,9 @@
(binding [*out* w]
(pr (-> o meta ::print))))

(defmethod print-method ::service [o ^Writer w]
(.write w (-> o meta :name str)))

(defn- try-namespace [x]
(when (ident? x)
(namespace x)))
Expand Down
23 changes: 23 additions & 0 deletions test/darkleaf/di/service_name_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(ns darkleaf.di.service-name-test
(:require
[clojure.test :as t]
[darkleaf.di.core :as di]))

(defn service [arg])

(t/deftest ok
(with-open [s (di/start `service)]
(conj @s 1))
,,)

;; It was:
;; class clojure.core$partial$fn__5908 cannot be cast to class
;; clojure.lang.IPersistentCollection (clojure.core$partial$fn__5908
;; and clojure.lang.IPersistentCollection are in unnamed module of
;; loader 'app'

(t/deftest ok2
(with-open [s (di/start `service)]
@s)
;; => #di/service[#'darkleaf.di.service-name-test/service]
,,)

0 comments on commit 66e373b

Please sign in to comment.