Page 1 of 2

How do I define a keyword as a integer constant?

Posted: Thu May 08, 2014 8:26 pm
by joeish80829
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:)

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

Posted: Fri May 09, 2014 11:08 am
by pjstirling
A keyword has one and only one value, itself as a symbol. There is no way to subvert this

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

Posted: Fri May 09, 2014 3:16 pm
by joeish80829
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.

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

Posted: Sat May 10, 2014 12:21 pm
by marcoxa
You want..... shameless plug....

http://defenum.sourceforge.net

Cheers

MA

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

Posted: Sat May 10, 2014 12:44 pm
by joeish80829
Where does it show how to define a keyword as a constant...can you show me an example?

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

Posted: Sun May 11, 2014 1:21 am
by marcoxa
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

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

Posted: Sun May 11, 2014 1:37 am
by joeish80829
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)

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

Posted: Sun May 11, 2014 1:18 pm
by marcoxa
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

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

Posted: Sun May 11, 2014 1:52 pm
by joeish80829
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?"

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

Posted: Mon May 12, 2014 12:48 am
by marcoxa
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