Thank you both, those are indeed good points. I'm not sure if I prefer coerce over cast, because (but I may be wrong), coerce implies to me some conversion. And, indeed, cast already exists in clojure, which I hadn't seen.
Is there a strong requirement for cast throwing an exception rather than just returning nil ? -- Laurent 2009/3/10 Christian Vest Hansen <karmazi...@gmail.com> > > On Tue, Mar 10, 2009 at 9:40 AM, Timothy Pratley > <timothyprat...@gmail.com> wrote: > > > > How about a cast variant that doesn't throw an exception but returns > > nil? > > > > user=> (cast java.lang.Boolean false) > > false > > user=> (cast java.lang.Integer false) > > java.lang.ClassCastException (NO_SOURCE_FILE:0) > > user=> (defn instance [c i] (try (cast c i) (catch Exception e))) > > #'user/instance > > user=> (instance java.lang.Boolean false) > > false > > user=> (instance java.lang.Integer false) > > nil > > That's a better idea. You might want to consider calling it 'coerce' > instead of 'instance' - I think that name is clearer and fits the > intent better. > > > > > > > Regards, > > Tim. > > > > > > On Mar 10, 7:15 pm, Christian Vest Hansen <karmazi...@gmail.com> > > wrote: > >> On Mon, Mar 9, 2009 at 11:10 PM, Laurent PETIT <laurent.pe...@gmail.com> > wrote: > >> > Hello, > >> > >> > I have the use case for calling instance? where, once instance? > returns > >> > true, I want to do something with the successful instance, such as > binding > >> > it, or directly calling something on it. > >> > >> > For instance, I have in my code : > >> > >> > (let [console (.getConsole v)] > >> > (when (instance? org.eclipse.debug.ui.console.IConsole console) > >> > ... ...) > >> > >> > Where I would like to really use when-let : > >> > (when-let [console (instance? org.eclipse.debug.ui.console.IConsole > >> > (.getConsole v))] > >> > ... ...) > >> > >> > For this to work, instance? would have to return logical true instead > of > >> > real true. > >> > >> > Do you think it could be an interesting idea to change instance? in > such a > >> > way ? Or maybe there's already something similar in clojure-contrib I > >> > haven't seen ? > >> > >> What about this case: > >> > >> (when-let [x (instance? java.lang.Boolean false)] (println x)) > >> > >> I think that should print 'true', and therefor I am against this > proposal. > >> > >> > >> > >> > Thanks in advance, > >> > >> > -- > >> > Laurent > >> > >> -- > >> Venlig hilsen / Kind regards, > >> Christian Vest Hansen. > > > > > > > > > -- > Venlig hilsen / Kind regards, > Christian Vest Hansen. > > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---