Well, I don't know anything built-in that you could use (except let , which you don't need). But the beauty is that you could build it yourself.
One idea would be to create a “new” defn , where the keyword :return left of the expression means returning the value from this, not the last expression. The macro will be appropriate here because we will create a modified expression (defn ...) that requires invaluable code.
(defmacro defn-return-middle [nm arg-vec & body-exprs] `(defn ~nm ~arg-vec ~(if (some #{:return} body-exprs) (let [[before _ [to-be-returned & after]] (partition-by #{:return} body-exprs)] `(let [ret
This extends something like:
(defn-return-middle f [] a :return b c)
Sort of:
(defn f [] (let [ret (do ab)] c ret))
For example, now you can:
(defn-return-middle blah [a] (+ 1 a) :return (+ 2 a) (println "does this work?"))
And now in REPL:
user> (blah 5) does this work? =>7
(Yay!)
Or for your example, you now do:
(defn-return-middle test1 [] (start-server) :return (run-pvt-and-expect "PVT-0") (stop-server))
True, the macro uses let , but it works by automating the let extension that you would have done if you wrote it manually each time. This means that now, using this macro, you will no longer unsubscribe let . In addition, this macro does not currently work on function definitions that declare several phenomena. But it would not be too difficult to change it to work for them.
source share