case statement and java constants

171 views
Skip to first unread message

Adam Clements

unread,
Feb 28, 2014, 11:15:01 AM2/28/14
to clo...@googlegroups.com
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

Ambrose Bonnaire-Sergeant

unread,
Feb 28, 2014, 11:21:32 AM2/28/14
to clojure
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.

Adam Clements

unread,
Feb 28, 2014, 11:40:49 AM2/28/14
to clo...@googlegroups.com
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

Ambrose Bonnaire-Sergeant

unread,
Feb 28, 2014, 11:47:03 AM2/28/14
to clojure
IMO that would needlessly complicate an otherwise simple construct, especially since a simple macro can endlessly customise it.

Thanks,
Ambrose

Herwig Hochleitner

unread,
Feb 28, 2014, 12:36:55 PM2/28/14
to clo...@googlegroups.com
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

Andy Fingerhut

unread,
Feb 28, 2014, 2:41:50 PM2/28/14
to clo...@googlegroups.com
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


--

Alex Miller

unread,
Feb 28, 2014, 8:58:08 PM2/28/14
to clo...@googlegroups.com
I would appreciate a jira enhancement ticket for this.

Alex

Herwig Hochleitner

unread,
Mar 2, 2014, 11:35:22 AM3/2/14
to clo...@googlegroups.com


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

Alex

Adam Clements

unread,
Mar 2, 2014, 11:51:39 AM3/2/14
to clo...@googlegroups.com
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

Herwig Hochleitner

unread,
Mar 2, 2014, 12:32:18 PM3/2/14
to clo...@googlegroups.com
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
Reply all
Reply to author
Forward
0 new messages