The issue is that #'arg
will always be a syntax object. An identifier is a kind of syntax object, so it makes sense to test (identifier? #’arg)
. However, (symbol? #’arg)
and (string? #’arg)
will always fail.
Suppose then that you invoke the macro with "1"
as the operand, it would fail every case in syntax-case
, so syntax-case
throws a bad syntax
(because a syntax transformation must be total), causing the error that you saw.
If you insist on using syntax-case
, then try this:
(define-syntax (foo stx)
(syntax-case stx ()
[(_ arg) (identifier? #'arg) #'1]
[(_ arg) (string? (syntax->datum #'arg)) #'2]
[(_ arg) (number? (syntax->datum #'arg)) #'3]
[(_ (quote* x)) (and (free-identifier=? #'quote* #'quote)
(symbol? (syntax->datum #'x))) #'4]
[(_ _) #'5]))
(foo a) ;=> 1
(foo "a") ;=> 2
(foo 10) ;=> 3
(foo 'a) ;=> 4
(foo (bar x)) ;=> 5
A better way though is to use syntax-parse
.
(require (for-syntax syntax/parse))
(define-syntax (foo stx)
(syntax-parse stx
#:literals (quote)
[(_ arg:id) #'1]
[(_ arg:string) #'2]
[(_ arg:number) #'3]
[(_ (quote x:id)) #'4]
[(_ _) #'5]))
(foo a) ;=> 1
(foo "a") ;=> 2
(foo 10) ;=> 3
(foo 'a) ;=> 4
(foo (bar x)) ;=> 5
--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/DED7E28E-9B3D-4045-B0DC-CBD3AB11E653%40gmail.com.
For more options, visit https://groups.google.com/d/optout.
On May 29, 2019, at 11:09 AM, Sorawee Porncharoenwase <sorawe...@gmail.com> wrote:(foo a) ;=> 1 (foo "a") ;=> 2 (foo 10) ;=> 3 (foo 'a) ;=> 4 (foo (bar x)) ;=> 5