libera/#commonlisp - IRC Chatlog
Search
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
15:40:54
younder
If done 'manually' it reads to unreadable code. If done mechanically you get java bloat but better speed.
15:47:45
younder
It is the compiler that should use inlining preferably automatically. The writer should focus on clear code. Macros obfuscate code. Fine for DRY and for creating higher level structures over tagbody if and the like.
15:55:46
NotThatRPG_
Macros like the with- constructs (e.g., with-open-file) reduce bloat and clarify the meaning of code, because you don't have to go hunting around to figure out when resources are cleaned up.
15:56:55
NotThatRPG_
They are also a good reason for macros because you are syntactically abstracting when you splice your resource variable name into the construct.
15:56:58
phadthai
I agree, macros can make code cleaner or messier, but for compiling optimization is where they risk making the code less readable indeed
15:58:28
NotThatRPG_
Keeping in focus the distinction b/w syntactic and semantic abstraction is key.
15:59:39
beach
What bike said: "using macros instead of functions is "for speed" an 80s C move that we don't need to promulgate" + modification
16:00:41
nij-
Oh..? Using macros was for speed mostly only for C (or did I misunderstood)? How come?
16:10:35
bike
it is very common to see C code that does things like "#define MIN(x, y) (x < y) ? x : y" instead of using the standard min function either because it is very old, or because it is new but the person who wrote it is unaware that compilers have improved since 1972.
16:16:54
beach
bike: Exactly. And in the case of some of my former students, it was a general lack of knowledge about how compilers work and what they are capable of. So they would just repeat what they had seen.
16:17:39
beach
This is why I keep repeating that good programmers must know about compiler design and computer architecture.
16:19:54
beach
Another habit my former students had was to avoid variable definitions in inner scopes, particularly in loops, believing that the program would then have to somehow create those variables each time the inner scope was entered.
16:30:03
bike
bad for readability plus makes the register allocator work harder to avoid spills. very nice