On Oct 3, 3:13 pm, Stuart Halloway <[EMAIL PROTECTED]> wrote:
> (defmacro ?.
> "like .. but drops out on null object"
> ([x form]
> `(. ~x ~form))
> ([x form & more]
> `(if-let x# (. ~x ~form) (.? x# [EMAIL PROTECTED]))))
Interesting -- I like it. It doesn't seem to be totally compatible
with "..", though:
user=> (?. System (getenv "SHELL") (startsWith "/bin") getClass
getName)
java.lang.Exception: Unable to resolve symbol: startsWith in this
context (NO_SOURCE_FILE:62)
Also, if-let could give the wrong result in the (admittedly unlikely)
situation that one of the methods returns Boolean.FALSE.
Here's another take:
(defmacro ?..
"like .. but stops and returns nil if any method returns nil"
([x form]
`(.. ~x ~form))
([x form & more]
`(let [x# (?. ~x ~form)]
(if (nil? x#) nil
(?. x# [EMAIL PROTECTED])))))
user=> (?.. System (getenv "SHELL") (startsWith "/bin") getClass
getName)
"java.lang.Boolean"
user=> (?.. System (getenv "FOO") (startsWith "/bin") getClass
getName)
nil
-the other Stuart
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---