【发布时间】:2018-01-19 14:19:01
【问题描述】:
我想编写对无类型球拍和有类型/球拍都正确的代码...
目前,我提出了以下不太令人满意的原则:我在文件开头插入了一个 ':' 的虚拟语法定义,以便在使用无类型球拍时跳过注释。我在使用 typed/racket 时评论了这个定义。
例如:
#lang racket
; comment the following line for typed/racket
(define-syntax : (syntax-rules () ((_ id type) (void))))
(: fact (-> Integer Integer))
; the rest of the file is common to both racket and typed/racket
(define (fact n) (if (zero? n) 1 (* n (fact (sub1 n)))))
和
#lang typed/racket
; comment the following line for typed/racket
;(define-syntax : (syntax-rules () ((_ id type) (void))))
(: fact (-> Integer Integer))
; the rest of the file is common to both racket and typed/racket
(define (fact n) (if (zero? n) 1 (* n (fact (sub1 n)))))
这样我只需要删除/插入一个';'在球拍和打字/球拍之间切换...
但是有没有办法让代码不需要任何更改就可以在球拍和打字/球拍中运行?我没有找到如何以编程方式检测我是否在球拍或打字/球拍中......然后我想我还必须找到一种方法来有条件地在顶层定义':'......所有这些都没有似乎没有办法,所以,有没有更好的办法?
【问题讨论】:
标签: racket