4 ms·
> To use with-open effectively, we must macroexpand it in our heads whenever it appears in the code. I'd like to add that if you find yourself needing to menta
by groovy2shoes 11y ago
> To use with-open effectively, we must macroexpand it in our heads whenever it appears in the code.
I'd like to add that if you find yourself needing to mentally expand a macro every time you use/read it, you have a poor abstraction. Consider going back to the drawing board and coming up with a good abstraction instead.
- lispm 11y agoRight. For a macro which only implements a 'simple' syntactic transformation, I would expect that I need to understand the semantics anyway. Additionally I need to understand the syntax it implements. What I don't want at all, is to macroexpand macro forms. If I need to macroexpand those, then I would do this for debugging, when there is something wrong during expansion or with the generated code. I would also do the expansion not in my head, but using a function called 'macroexpand'. For example as a reader, I don't want to macroexpand the following form in my head: (with-open-file (foo "bar.text") (read foo)) What I really want is to understand the syntax of WITH-OPEN-FILE and semantics of such a form. To what it transforms itself is the least important thing I (as a code reader) want to know when dealing with macro forms. * it's a WITH- type macro which sets up a scope. * a WITH- type macro usually expects a variable, something which helps creating an object that is bound to the variable and a bunch of options (foo-var "the-file-name.text" :direction :input) * it then expects a body, a sequence of forms, where the variable FOO-VAR is bound to a file That's the syntax. The semantics is that at runtime it opens a file as a stream and makes sure that on all forms of exit, the stream gets closed. To what form this actually expands (even if it is a simple transformation) is uninteresting and may be different from implementation to implementation.