【发布时间】:2019-12-27 12:04:02
【问题描述】:
是否可以定义一个与球拍中的函数同名的宏,但不能覆盖它。 (因此该名称将有 2 种可能的用法,具体取决于传递的参数)
例如函数round。
1.用法(球拍功能)
( round 1.2 )
-> 1
2.用法(我们的宏)
(round (someStruct arg))
-> resultStruct
【问题讨论】:
是否可以定义一个与球拍中的函数同名的宏,但不能覆盖它。 (因此该名称将有 2 种可能的用法,具体取决于传递的参数)
例如函数round。
1.用法(球拍功能)
( round 1.2 )
-> 1
2.用法(我们的宏)
(round (someStruct arg))
-> resultStruct
【问题讨论】:
(require (only-in racket [round @round]))
(define (my-round v)
;; do whatever you want to do here
"hello")
(define (round v)
(cond
[(number? v) (@round v)]
[else (my-round v)]))
(define x 1.2)
(round x) ;=> 1.0
(define y "abc")
(round y) ;=> "hello"
通过将round 定义为一个函数,对在运行时 传递给函数的值 进行案例分析。
(require syntax/parse/define
(only-in racket [round @round]))
(define-syntax-parser round
[(_ x:number) #'(@round x)]
[(_ x) #'"hello"])
(define x 1.2)
(round x) ;=> "hello"
(define y "abc")
(round y) ;=> "hello"
(round 1.2) ;=> 1.0
(round (this is not a sensible expression but it works (()))) ;=> "hello"
通过将round 定义为宏,对在编译时 传递到宏的语法片段 进行案例分析。在上面的示例中,当宏的操作数是文字数字时,我们将使用实际 Racket 的round。其他所有内容都将转换为"hello"。请注意,在编译时,x 之类的标识符尚未评估并与值关联。你唯一知道的是它是一个标识符,它不是一个字面数字,所以它转换为"hello"。这个宏展开后的程序大概是:
(require syntax/parse/define
(only-in racket [round @round]))
(define x 1.2)
"hello"
(define y "abc")
"hello"
(@round 1.2)
"hello"
选择你喜欢的那个。我怀疑你实际上想使用函数而不是宏。
【讨论】: