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


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.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to