medley icon indicating copy to clipboard operation
medley copied to clipboard

Add ?? macro. It's like `clojure.core/or` but for nil's.

Open darkleaf opened this issue 2 years ago • 3 comments

darkleaf avatar Nov 09 '21 18:11 darkleaf

Thank you for the patch, but I think this is too specific for Medley.

weavejester avatar Nov 09 '21 23:11 weavejester

@darkleaf Not sure what your use case is but have you considered:

(m/find-first some? ...)

If I'm reading your test cases correctly I think the only case the above doesn't cover is:

(is (= :ok (m/?? :ok (throw (ex-info "Fail" {})))))`

anthonygalea avatar Nov 10 '21 16:11 anthonygalea

@anthonygalea I want to use ?? to set up lazy defaults:

(fn [x y z]
  (m/?? x (costly-get-x y z) (throw (ex-info "Can't get x" {:y y, :z z}))))

(fn [x y z]
  (m/?? x (costly-get-x y z) default-value))

(m/?? (get-in x [...])
      (my-func ...)
      (get-in y [...])
      (throw ...))        

darkleaf avatar Nov 11 '21 08:11 darkleaf