Arc Forumnew | comments | leaders | submitlogin
2 points by conanite 5277 days ago | link | parent

The great thing about macros is that they can inspect their arguments and decide what to do with them; this is what anarki's 'def does in order to generate doc-strings. If the first element of the 'body arg is a string, use that to document the function currently being defined. Similarly, we could say if the first element of paginate's 'body arg is, say, a hash, use that hash as keyword args, and use (cdr body) for the body.

Using {...} syntax for the hash, this could even be elegant:

  (paginate '/search' 10
    { nextcopy "Next →" prevcopy "← Prev" }
    (each doc (cut docs start-index end-index)
      (render-doc doc)))
Without {...} you would have to fall back to some kind of keyword, but even so I would prefer

  (paginate '/search' 10
    (opts nextcopy "Next →" prevcopy "← Prev" )
    (each doc (cut docs start-index end-index)
      (render-doc doc)))
, in this case 'paginate checks if the first element of its 'body arg is a list beginning with 'opts. Something like the following (completely untested)

  (mac paginage (url numitems . body)
    (let (opts body) (extract-opts body)
      (etc etc)))

  (def extract-opts (body)
    (if (is (caar body) 'opts) 
        (list (cdar body) (cdr body))
        (list nil body)))
Would you consider this compromises the rest parameters?


3 points by akkartik 5277 days ago | link

Yes, I thought about adding a set of parens around either keyword or rest params. The brace syntax sounds decent. I think it works just as well; it's just a question of what syntax people prefer.

You'd still need support for defaults in extract-opts. Complexity-wise I think the two implementations would be equivalent.

Hmm, one benefit of using a separator like :do: In macros that need just keyword args the opts approach would add redundant syntax to each call.

I don't understand the entire state space here, but I used to think arguments should go into rest params by default, and now I think they should go into keyword args. What do you think?

-----