why is it necessary to use identity to check for nils in an if statement
this doesn't work: user= (defn if-a [a b] (if (a) (str a) (str b))) #'user/if-a user= (if-a nil b) java.lang.NullPointerException (NO_SOURCE_FILE:0) user= (if-a a nil) user= java.lang.ClassCastException: java.lang.String cannot be cast to clojure.lang.IFn (NO_SOURCE_FILE:0) this does work: user= (defn if-a [a b] (if (identity a) (str a) (str b))) #'user/if-a user= (if-a nil b) b user= (if-a a nil) a why is the identity function is needed? thanks -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: why is it necessary to use identity to check for nils in an if statement
You have an extra set of parens around a, treating it as a function call. Try: (defn if-a [a b] (if a (str a) (str b))) Hope that helps, Dave On Tue, Aug 23, 2011 at 4:37 PM, Andrew Xue and...@lumoslabs.com wrote: this doesn't work: user= (defn if-a [a b] (if (a) (str a) (str b))) #'user/if-a user= (if-a nil b) java.lang.NullPointerException (NO_SOURCE_FILE:0) user= (if-a a nil) user= java.lang.ClassCastException: java.lang.String cannot be cast to clojure.lang.IFn (NO_SOURCE_FILE:0) this does work: user= (defn if-a [a b] (if (identity a) (str a) (str b))) #'user/if-a user= (if-a nil b) b user= (if-a a nil) a why is the identity function is needed? thanks -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: why is it necessary to use identity to check for nils in an if statement
user= (defn if-a [a b] (if (a) (str a) (str b))) The problem is (a) - it tries to call a as a function, which throws NullPointer if a is nil. You meant: user= (defn if-a [a b] (if a (str a) (str b))) mg On Tue, Aug 23, 2011 at 9:37 PM, Andrew Xue and...@lumoslabs.com wrote: this doesn't work: user= (defn if-a [a b] (if (a) (str a) (str b))) #'user/if-a user= (if-a nil b) java.lang.NullPointerException (NO_SOURCE_FILE:0) user= (if-a a nil) user= java.lang.ClassCastException: java.lang.String cannot be cast to clojure.lang.IFn (NO_SOURCE_FILE:0) this does work: user= (defn if-a [a b] (if (identity a) (str a) (str b))) #'user/if-a user= (if-a nil b) b user= (if-a a nil) a why is the identity function is needed? thanks -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en