Has anyone made a try+ meander yet?
try+?
Like slingshot.
I was thinking this would be pretty dope:
(try+
(add-user! user)
(catch {:type ::data.user/user-exists :id ?user-id}
{:message (format "Oh no, %s user exists!" ?user-id)})
(catch {::anom/category :forbidden}
{:message "GO AWAY"})
(catch SQLException e
{:message "Write better SQL, Idiot"}))
What's the "match anything" or what's the "else" for m/match?
Quite enjoying this, because I can write naively and optimize the macro output
Match anything is _
Or if you want to capture it, you can just do any any ?logic_variable
.
LOL I need more messages like “Write better SQL, Idiot” in my life.
I can’t remember which command it is/was but it would complain about a “Cowardly attempt to…”
Just calling me out.
For me its Cowardly refusing to create an empty archive from tar 🙂
Hmm.. (if (= (System/getenv "USER") "noprompt") (throw ...))
...
HAHAHA! Yeah! Thats the one! 😂
For some reason I remembered it as calling me the coward. 😛
Okay… I’m slowly getting my head out of the vacation clouds.
It's 70loc, but I have it...
Something that's a little tricky is the fact you have to "jump" priorities a little bit.
This is fully functional though:
(try+
(throw (ex-info "Invalid" {:type :pity :reason "???"}))
(read (clojure.lang.LineNumberingPushbackReader. (java.io.StringReader. ")")))
(throw (ex-info "Invalid" {:type :invalid :reason "I pity the fool"}))
(/ 1 0)
(catch ArithmeticException _
::infinity-stones)
(catch clojure.lang.LispReader$ReaderException {:clojure.error/line ?line}
(println "Check line" ?line))
(catch {:type :invalid :reason ?reason}
(prn ?reason)
nil)
(catch {:type :pity :reason ?reason}
(println "PITYPITYPITY" ?reason)
nil)
(catch clojure.lang.ExceptionInfo e
(prn (ex-data e)))
(finally (println "Cleaning up")))
Pulled it off with rewrite?
Didn't need it at all in the end. Rewrite made it more complicated tbh, because you have to look ahead. I'm sure you'll show me the one-liner in a sec though ;)
I'll give it a real name later, but it seems like a neat idea & works in the REPL.
Nice. 🙂
I kinda wanna mess around with this.
Do it. I consider the idea primitive, so if you want to build on top of it, go to town. I don't feel any particular ownership.
Meander has a great api for simple cases, like matching a few keys in a map on value. It makes this kind of lightweight thing really nice. The only real catch is that I need to specify that you need to use epsilon in order to interact with it, as it's incompatible with the other versions. But that's not a big deal really, eventually it'll be stable and I can "meander" rather than caveating