This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-10-26
Channels
- # aws (1)
- # beginners (15)
- # boot (30)
- # cider (13)
- # cljsrn (16)
- # clojure (458)
- # clojure-dev (15)
- # clojure-france (131)
- # clojure-greece (124)
- # clojure-korea (2)
- # clojure-spec (42)
- # clojure-uk (115)
- # clojure-ukraine (1)
- # clojurescript (103)
- # component (18)
- # cryogen (1)
- # datomic (4)
- # dirac (3)
- # figwheel (1)
- # funcool (13)
- # hoplon (60)
- # luminus (2)
- # off-topic (2)
- # om (28)
- # onyx (45)
- # parinfer (28)
- # pedestal (1)
- # proton (23)
- # re-frame (18)
- # reagent (36)
- # ring (1)
- # ring-swagger (5)
- # untangled (13)
- # vim (9)
Hey. Is there core function that returns nil
for any number of args, ie something like:
(defn nil-foo [&args])
Or maybe I’m doing it wrong, as I need this for
((or (resolve 'function-that-might-not-exist) nil-foo) "args" "to" "resolved" "function”)
Any reason you can't just wrap the fn call in a when
?
(when function-that-might-not-exist
(function-that-might-not-exist "args" "to" "function"))
@pawel.kapala Yeah when-let
should be your friend
that’s good, but when function-that-might-not-exist
does not exist I get RuntimeException
that function-that-might-not-exist
does not exist 😆 and I want nil
to be returned in that case, I wouldn’t (try) (catch)
would I? :X
I think I'd need to see more of your code. The when
tests for if the symbol (`function-that-might-not-exist` in this case) is nil or not and only invokes the fn if it's not nil otherwise a when returns nil
e.g.
(defn invoke-fn-that-may-not-exist
[fn]
(when fn
(fn "output this")))
(defn echo
[x]
(str x))
(invoke-fn-that-may-not-exist echo) ;; => "output this"
(invoke-fn-that-may-not-exist dummy) ;; => nil
but in this case all the symbols you’re using are defined, maybe I should’ve wrote function-that-might-not-be-defined
instead of function-that-might-not-exist
😉
see this (`foo-bar` is not defined above):
(when foo-bar
(foo-bar "args" "to" "resolved" "function"))
CompilerException java.lang.RuntimeException: Unable to resolve symbol: foo-bar in this context, compiling:(/private/var/folders/pq/jrskhrwn5jl_yknn7jc5jkt00000gn/T/form-init8702595223277930847.clj:1:1)
dummy
is not defined in the edited example
ok I get you now 🙂 dummy
is passed as fn param and is bound inside, that’s why it works 😆
if you don't have a symbol defined you can't reference it although you could use your resolve
in the when or even better a when-let
thanks @agile_geek! I’m gonna go with when
as it’s simple and clear.