libera/#commonlisp - IRC Chatlog
Search
3:43:01
jhiltu
I've tried using cl a bit but I've never really understood macros, when to use them or how to write them. Now I'm attempting to play around with cl-sdl2 a bit and I wonder if I could somehow write a macro for defining keybindings like this? https://github.com/jhiltu/cl-sdl2-test-project/blob/main/keys.lisp
3:43:24
jhiltu
this is the current main (and only) file.. it's a barebones skeleton for cl-sdl2 project https://github.com/jhiltu/cl-sdl2-test-project/blob/main/test-project.lisp
3:44:57
beach
jhiltu: Macros are used to create new syntax, not at the character level (for which there are reader macros) but at the s-expression level.
3:45:30
beach
jhiltu: Creating new syntax like that might decrease the amount of boilerplate code that would otherwise be required.
4:20:01
aeth
jhiltu: Imo... The most common, simple macros that you may want to use or define probably fit the define-foo, with-foo, or do-foo patterns. In other words, define a global with certain boilerplate removed (probably a wrapper over defclass or defun... rarely perhaps something like deftype or defparameter), or define a local (quite frequently with unwind-protect), or define a custom iteration.
4:22:10
aeth
for instance, Practical Common Lisp has a do-primes as its example. https://gigamonkeys.com/book/macros-defining-your-own
8:15:16
jhiltu
I just don't understand macros :'( I tried looking at a million examples online but it never works :D https://gist.github.com/jhiltu/acd149b2f91f6bfffc17afe529702b27
8:19:18
beach
jhiltu: The arguments to a macro are not evaluated. They are forms. So when you type (DEF-KEYS-CLASS FOO MY-KEYS), then MY-KEYS is going to be passed as a symbol to the macro expander.
8:23:33
beach
Wanting to force evaluation is a sign of that. But why do you want to pass the keylist as an argument? Are you going to define more than one class with the same list of keys?
8:26:37
beach
I have no easy solution, but that's probably because I am preoccupied with other stuff. Maybe someone else can figure something out.
8:38:37
beach
Yes, indeed. If someone comes up with a reasonable solution, I think that will be the case. But I am not very concentrated right now.
9:10:49
beach
But, yes, you can force evaluation using EVAL, or in this case, you may be able to use just SYMBOL-VALUE.
9:24:53
jack_rabbit
Hello! I have a small macro here to enable continuous retrying of a form via restart. I'm curious if there's a better way to do this: https://plaster.tymoon.eu/view/4249#4249
9:39:37
younder
If there is only one parameter why use key. You could use &boy instead of &rest. Something like (defmacro with-retry (report &optional (restart-name 'retry) &body body)...
11:29:57
jack_rabbit
younder, Thanks, good idea. But other than the argument style, the implementation looks reasonable?
13:10:53
jhiltu
I ended up abandoning the whole idea of classes and macros and things I don't understand... and just used property lists in case someone's curious https://gist.github.com/jhiltu/be20d1b1f424d6e33ad9c15804d4e740
15:16:19
nij-
jhiltu Yeah. alphapapa told me that the point of macros is to abstract syntactically. One lesson I learned from this is to first write codes in functions, and if you spot repeating codes, abstract them in macros.
15:24:56
NotThatRPG
nij-: It's a subtle issue to decide whether to abstract using a macro or a function...
15:28:22
younder
A macro is substituted in directly so it is faster. Of course you could make the function inline..
15:31:36
bike
macros are nice when you actually want new syntax. for example, dolist and mapc do the same thing, and either could be implemented in terms of the other, but it's sometimes nice to just have a body right there
15:33:44
younder
Optimizing compilers like Java's use aggressive function expansion and then register intimation. It is provably best to lave it to the compiler. But yes
15:36:26
NotThatRPG_
younder: Right, but remember that macros can't be invoked with funcall or apply
15:37:13
bike
using macros instead of functions is "for speed" an 80s C move that we don't need to promulgate