Consider a macro that works just like `define`, but only allows function
definitions. Here’s a simple implementation using the function-header syntax
class from syntax/parse/lib:

  (define-syntax (defn stx)
    (syntax-parse stx
      [(_ header:function-header body ...+)
       #'(define header body ...)]))

This works, but the error reporting isn’t very good when the body is empty:

  (defn (f a b))
  ; defn: bad syntax in: (defn (f a b))

However, if I manually adjust the form to use ... instead of ...+, the error
is much nicer:

  (define-syntax (defn stx)
    (syntax-parse stx
      [(_ header:function-header body0 body ...)
       #'(define header body0 body ...)]))
  
  (defn (f a b))
  ; defn: expected more terms starting with any term

This gets much nicer if I include a ~describe form:

  (define-syntax (defn stx)
    (syntax-parse stx
      [(_ header:function-header
          (~describe "expression or internal definition" body0) body ...)
       #'(define header body0 body ...)]))
  
  (defn (f a b))
  ; defn: expected more terms starting with expression or internal definition 

Is there any trick to get this kind of error reporting when using ...+? Would
it makes sense to improve the reporting for ...+ to do something like this,
or are there edge cases I have no anticipated that would make this invalid?

Thanks,
Alexis

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to