How do I define a keyword as a integer constant?

Discussion of Common Lisp
joeish80829
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

How do I define a keyword as a integer constant?

Post by joeish80829 » Thu May 08, 2014 8:26 pm

Now I use the SWIG defanonenum:

Code: Select all

(defmacro defanonenum (&body enums)
  "Converts anonymous enums to Lisp constants."
  `(cl:progn ,@(cl:loop for value in enums
			for index = 0 then (cl:1+ index)
			when (cl:listp value) 
			do (cl:setf index (cl:second value)
				    value (cl:first value))
			collect `(cl:defconstant ,value ,index))))

to define my constants. I usually define like this with the + signs on either end:

Code: Select all

(defanonenum

(+char-max+ 127))

I would like to have constants that are keywords so instead of +char-max+ it would be :char-max.
The only caveat is that the speed would closely match what I have now. A billion runs of +char-max+
takes 0.404 seconds of real time in my current setup. Thank you in advance for any guidance on this:)

pjstirling
Posts: 166
Joined: Sun Nov 28, 2010 4:21 pm

Re: How do I define a keyword as a integer constant?

Post by pjstirling » Fri May 09, 2014 11:08 am

A keyword has one and only one value, itself as a symbol. There is no way to subvert this

joeish80829
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

Re: How do I define a keyword as a integer constant?

Post by joeish80829 » Fri May 09, 2014 3:16 pm

Actually CFFI has a way but it is slow. If you have CFFI installed you can go like this:


(cffi:defcenum :mouse-event-enum
(:cv-event-mouse-move 0))

(cffi:foreign-enum-value :mouse-event-enum :cv-event-mouse-move )

and the output is 0

so in a function that accepts a constant as an &optional value:

The &optional would be this:

&optional (mouse-type :cv-event-mouse-move) for example

and when the constant was calculated in function...you would have to use it like this,

(cffi:foreign-enum-value :mouse-event-enum :cv-event-mouse-move )

to get the value. but when a user uses the constant all he would have to enter is the :cv-event-mouse-move which I think looks really nice. Any ideas on how to make this happen without it being slow at all.

marcoxa
Posts: 85
Joined: Thu Aug 14, 2008 6:31 pm

Re: How do I define a keyword as a integer constant?

Post by marcoxa » Sat May 10, 2014 12:21 pm

You want..... shameless plug....

http://defenum.sourceforge.net

Cheers

MA
Marco Antoniotti

joeish80829
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

Re: How do I define a keyword as a integer constant?

Post by joeish80829 » Sat May 10, 2014 12:44 pm

Where does it show how to define a keyword as a constant...can you show me an example?

marcoxa
Posts: 85
Joined: Thu Aug 14, 2008 6:31 pm

Re: How do I define a keyword as a integer constant?

Post by marcoxa » Sun May 11, 2014 1:21 am

You cannot. There is no "direct" way to associate a "value" to a keyword without some indirection.

Keywords are keywords. Period.

The question is what are you trying to do.

Cheers
--
MA
Marco Antoniotti

joeish80829
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

Re: How do I define a keyword as a integer constant?

Post by joeish80829 » Sun May 11, 2014 1:37 am

Well I'm wrapping a c++ library in lisp and I think it would be nice if I could call a function that needs a constant like this:

(c++-function val1 val2 :constant)

rather than

(c++-function val1 val2 +constant+)

can't use it though if it takes 1.6 seconds for a million runs like this does:

(cffi:foreign-enum-value :mouse-event-enum :cv-event-mouse-move)

marcoxa
Posts: 85
Joined: Thu Aug 14, 2008 6:31 pm

Re: How do I define a keyword as a integer constant?

Post by marcoxa » Sun May 11, 2014 1:18 pm

Well, by now we all know that you are wrapping a C++ library. What is the problem of using a constant that you initialized at the proper value throughout your code?

Cheers
--
MA
Marco Antoniotti

joeish80829
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

Re: How do I define a keyword as a integer constant?

Post by joeish80829 » Sun May 11, 2014 1:52 pm

I just like the way the keywords look(:char-max) as opposed to this +char-max+. Especially since they are highlighted. I'm not sure I understood this " What is the problem of using a constant that you initialized at the proper value throughout your code?"

marcoxa
Posts: 85
Joined: Thu Aug 14, 2008 6:31 pm

Re: How do I define a keyword as a integer constant?

Post by marcoxa » Mon May 12, 2014 12:48 am

joeish80829 wrote:I just like the way the keywords look(:char-max) as opposed to this +char-max+. Especially since they are highlighted. I'm not sure I understood this " What is the problem of using a constant that you initialized at the proper value throughout your code?"
"Looks" are deceiving. If you want to use the keyword, which is a purely Lisp side object, you'll have to live with the CFFI foreign lookup function. Or, you just set the constant

Code: Select all

  (defconstant +foo+ #.(cffi:foreign-enum-value my-c++-enum-type :foo))
All in all it would be easier to fix the Emacs/slime highlighting of constants... (You are using Emacs, aren't you?)

Cheers
--
MA
Marco Antoniotti

Post Reply