Hi, 2009/5/4 Rich Hickey <richhic...@gmail.com> > > > > On May 4, 1:53 am, Laurent PETIT <laurent.pe...@gmail.com> wrote: > > 2009/5/4 Christophe Grand <christo...@cgrand.net> > > > > > > > > > > > > > Janico Greifenberg a écrit : > > > > Hi, > > > > > > I encountered unexpected behavior of the 'if' form in clojure when using > > > > instances of java.lang.Boolean as the condition. I wanted to convert > > > > input strings to booleans and used the constructor of the Boolean class > > > > with the string parameter. However, when I pass these values as a > > > > condition to if, the true-branch always gets executed. For example: > > > > > > > (if (Boolean. "true") 1 2) > > > > 1 > > > > > (if (Boolean. "false") 1 2) > > > > 1 > > > > > > It seems to me that this has to do with the identity of the objects, as > > > > (Boolean. "false") is not identical (although equal) to the clojure > > > > literal false. Is this behavior intentional or a bug? > > > > > > The problem does not occur when I use Boolean/parseBoolean which returns > > > > a lower case boolean. > > > > > It's intentional, Rich said "for efficiency only canonic false is > > > logical false in > > > Clojure." inhttp://groups.google.com/group/clojure/msg/81ba3175da9a877c > > > > > Workaround: > > > > (if (boolean (Boolean. "false")) 1 2) > > > 2 > > > > At the cost of a lesser experience concerning java interoperability, then ? > > > > Isn't that "premature optimization" ? ;-) > > Not at all. The cost would apply to all usage of 'if' (i.e. all > conditionals).
I don't know whether the impact would be that big, it was just a question. But whatever, shouldn't this be clearly stated in the documentation of the 'if' special form ( http://clojure.org/special_forms#toc2 ) ? "For performance reasons, 'if performs java instance comparison, so only Boolean instances that are equal to Boolean.FALSE will be matched as false (so one should only use Boolean/valueOf for creating Boolean instances)". (of course, I agree with the fact that Boolean should be used via the static valueOf method rather than the constructor) > > The Boolean ctor is documented as a 'do not use' item: > > http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Boolean.html#Boolean(boolean) The Boolean ctor is not documented as 'do not use', but 'it is rarely appropriate to use'. Doesn't that make a difference ? --~--~---------~--~----~------------~-------~--~----~ 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 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 -~----------~----~----~----~------~----~------~--~---