diff --git a/index.html b/index.html index c29adfe..3ce6c7b 100644 --- a/index.html +++ b/index.html @@ -187,8 +187,10 @@ audience I’m writing for, you don’t know them yet. You can skip learning them now. (Someday if you want to understand someone else’s older macros, you can learn about them then.)

The syntax variation of them works similarly. The idea is, we’ll define it to mean an error by default. Only inside of our -aif will it have a meaningful value:

> (require racket/stxparam)
> (define-syntax-parameter it
    (lambda (stx)
      (raise-syntax-error (syntax-e stx) "can only be used inside aif")))
> (define-syntax-rule (aif condition true-expr false-expr)
    (let ([tmp condition])
      (if tmp
          (syntax-parameterize ([it (make-rename-transformer #'tmp)])
            true-expr)
          false-expr)))
> (aif 10 (displayln it) (void))

10

> (aif #f (displayln it) (void))

We can still use it as a normal variable name:

> (define it 10)
> it

10

If we try to use it outside of an aif form, and -it isn’t otherwise defined, we get an error like we want:

> (displayln it)

10

Perfect.

6 Robust macros: syntax-parse

TO-DO.

7 Other questions

Hopefully I will answer these in the course of the other sections. But +aif will it have a meaningful value:

> (require racket/stxparam)
> (define-syntax-parameter it
    (lambda (stx)
      (raise-syntax-error (syntax-e stx) "can only be used inside aif")))
> (define-syntax-rule (aif condition true-expr false-expr)
    (let ([tmp condition])
      (if tmp
          (syntax-parameterize ([it (make-rename-transformer #'tmp)])
            true-expr)
          false-expr)))
> (aif 10 (displayln it) (void))

10

> (aif #f (displayln it) (void))

If we try to use it outside of an aif form, and +it isn’t otherwise defined, we get an error like we want:

> (displayln it)

it: can only be used inside aif

But we can still define it as a normal variable:

> (define it 10)
> it

10

6 Robust macros: syntax-parse

TO-DO. +TO-DO. +TO-DO.

7 Other questions

Hopefully I will answer these in the course of the other sections. But just in case:

7.1 What’s the point of with-syntax?

Done.

7.2 What’s the point of begin-for-syntax?

TO-DO.

7.3 What’s the point of racket/splicing?

TO-DO.

8 References/Acknowledgments

Eli Barzliay wrote a blog post, Writing ‘syntax-case’ Macros, which explains many key details. However it’s written