case statement and java constants

Showing 1-10 of 10 messages
case statement and java constants Adam Clements 2/28/14 8:15 AM
I'm having some trouble with java constants in a case statement. I know I could use condp, but these are things I could put in a java switch statement and so it's annoying to give up constant time dispatch:

(case (.getActionMasked event)
   MotionEvent/ACTION_POINTER_DOWN :down
   MotionEvent/ACTION_UP           :up
   MotionEvent/ACTION_POINTER_UP   :up
   MotionEvent/ACTION_MOVE         :move
   MotionEvent/ACTION_CANCEL       :cancel
   MotionEvent/ACTION_OUTSIDE      :outside
   :none))

Always gives me :none because java constants aren't literals, it dispatches on the symbol instead. Is there a way to do this without resorting to condp = or am I stuck? Should this be a something that case handles by default? I can't think of a reason these couldn't be converted to literals...

Thanks,

Adam
Re: case statement and java constants Ambrose Bonnaire-Sergeant 2/28/14 8:21 AM
I'm not 100% sure if this works, but have you tried writing a macro that gets the
Java field value, and inserting into the case statement?

(defmacro motion-case [...]
  `(case ..
     ~MotionEvent/ACTION_POINTER_DOWN ...
  ~ MotionEvent/ACTION_UP ...
))


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Re: case statement and java constants Adam Clements 2/28/14 8:40 AM
That works perfectly, thanks!

It does feel like it should just work as a normal case statement though, where the value is static final, and warn if it isn't. Is there a good reason this shouldn't be supported by the case statement that anyone can think of? Worth filing an enhancement request on clojure core?

Thanks,
Adam


Adam
Re: case statement and java constants Ambrose Bonnaire-Sergeant 2/28/14 8:47 AM
IMO that would needlessly complicate an otherwise simple construct, especially since a simple macro can endlessly customise it.

Thanks,
Ambrose
Re: case statement and java constants Herwig Hochleitner 2/28/14 9:36 AM
2014-02-28 17:21 GMT+01:00 Ambrose Bonnaire-Sergeant <abonnair...@gmail.com>:
(defmacro motion-case [...]
  `(case ..
     ~MotionEvent/ACTION_POINTER_DOWN ...
     ~MotionEvent/ACTION_UP ...
))

That's a neat trick! Didn't know case could do this.

Wouldn't it be great to mention this in the docs + the class of values for which this is legal, i.e. roughly those with a stable hash?

cheers
Re: case statement and java constants Andy Fingerhut 2/28/14 11:41 AM
Anyone who has a good working example of this can add it to clojuredocs.org in a few minutes, if they are so inclined:

    http://clojuredocs.org/clojure_core/clojure.core/case

I know that within 5 seconds of reading this one or more people will complain that the site is out of date.  It is true that its doc strings and source code are for Clojure 1.3, but the examples are free form and can say whatever you want, and there is very useful info in those examples that applies to Clojure 1.4, 1.5.1, and the forthcoming 1.6 just as well.

Andy


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Re: case statement and java constants Alex Miller 2/28/14 5:58 PM
I would appreciate a jira enhancement ticket for this.

Alex

Re: case statement and java constants Herwig Hochleitner 3/2/14 8:35 AM


2014-03-01 2:58 GMT+01:00 Alex Miller <al...@puredanger.com>:
I would appreciate a jira enhancement ticket for this.

Alex

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Re: case statement and java constants Adam Clements 3/2/14 8:51 AM
Oops, I already filed http://dev.clojure.org/jira/browse/CLJ-1367. Should probably link/mark as dupe - I don't appear to be able to do anything other than comment.


Adam
Re: case statement and java constants Herwig Hochleitner 3/2/14 9:32 AM
Oh sorry, I should have looked at the newest issues first.

I don't think the tickets are dupes, because yours calls for enabling java static fields for case constants.
Mine just wants to add language stating how case can be used right now.

IMO enabling case constants is at odds with current doc language about symbols aswell as not evaluating case constants, hence shoudn't be done, but I'll leave that decision to maintainers.

cheers