提问人:Tom Price 提问时间:11/10/2023 最后编辑:Tom Price 更新时间:11/10/2023 访问量:44
我的 Racket 异常处理宏找不到错误源
My Racket exception handling macro does not locate source of error
问:
我正在尝试实现参数化错误处理。如果发生错误且参数 SAFE 为 true,则会引发错误,程序将停止。但是,如果 SAFE 为 false,则会打印警告并继续程序。
#lang racket
(require syntax/parse/define)
;; raises parameterized error message
(define-simple-macro
(err param:id msg)
(if (param)
(error msg)
(display (format "Here be dragons: ~a\n" msg))))
;; a problematic procedure
(define (bad-thing?) (thunk #t))
;; default value of SAFE = #t
(define SAFE (make-parameter #t))
;; prints warning
(parameterize ([SAFE #f])
(when (bad-thing?)
(err SAFE "better watch out.")))
;; raises error
(when (bad-thing?)
(err SAFE "Hell's bells, an exception has occurred!"))
宏的问题在于,当引发错误时,它会追溯到定义宏的位置,而不是调用有问题的过程的位置。err
bad-thing?
一定有更好的方法!有人能给我指出正确的方向吗?
答:
2赞
Shawn
11/10/2023
#1
如果使用基包装器而不是更高级别的包装器来获取表示宏调用站点的语法对象,则可以包装 in syntax/loc
以使该位置显示为:syntax-parse
error
#lang racket
(require (for-syntax syntax/parse))
;; raises parameterized error message
(define-syntax (err stx)
(syntax-parse stx
[(err param:id msg)
#`(if (param)
#,(syntax/loc stx (error msg))
(display (format "Here be dragons: ~a\n" msg)))]))
;; a problematic procedure
(define (bad-thing?) (thunk #t))
;; default value of SAFE = #t
(define SAFE (make-parameter #t))
;; prints warning
(parameterize ([SAFE #f])
(when (bad-thing?)
(err SAFE "better watch out.")))
;; raises error
(when (bad-thing?)
(err SAFE "Hell's bells, an exception has occurred!"))
评论
0赞
Tom Price
11/11/2023
完善。谢谢!
上一个:在宏中捕获单态泛型
下一个:如何在 Cmake 中定义宏列表
评论
err
(define (err param msg) (if (param) ...
给出的结果大致相同。(when (bad-thing?) ...)
err
error