This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

How do I define a keyword as a integer constant?

15 posts · 16378 views

Now I use the SWIG defanonenum:
(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:
(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?

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?

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?

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?

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

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

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?

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

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

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?

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
  (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

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

You mentioned "You cannot. There is no "direct" way to associate a "value" to a keyword without some indirection" is there a way inolving redirection that would be under .10 seconds per million iterations...I could sure use an example

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

joeish80829 wrote:You mentioned "You cannot. There is no "direct" way to associate a "value" to a keyword without some indirection" is there a way inolving redirection that would be under .10 seconds per million iterations...I could sure use an example
I go by the maxim "first get it right, then get it fast". What you want is the constant which you initialize beforehand. It's the right way to do it. Forget about the keyword (or hack Emacs/Slime highlighting).

If you want you can do
(defenum foo ((baz #.(cffi:foreing-enum-value 'foo :baz))) ; etc
Now you will have a mapping to the "constant" BAZ. AFAIU It cannot become faster than that.

Cheers
--
MA
Marco Antoniotti

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

How would I fix emacs highlighting to see constants highlighted...also the package prefixes aren't getting highlighted e.g. gc:test, the gc: is the same color as "test"

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

How would I fix emacs highlighting to see constants highlighted...also the package prefixes aren't getting highlighted e.g. gc:test, the gc: is the same color as "test"

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

joeish80829 wrote:How would I fix emacs highlighting to see constants highlighted...also the package prefixes aren't getting highlighted e.g. gc:test, the gc: is the same color as "test"
Well, well, well... I don't know. I just know it is possible. The only enlightened and wise answer is thus: RTFM (either Emacs highlighting, or ask the slime guys) :mrgreen:
Marco Antoniotti