将强制转换应用于类型化球拍中动态需要的功能

迈克尔·麦克劳德

我正在尝试在运行时从其他模块加载和使用功能。问题是,dynamic-require范围Any似乎无法cast修改为更具体的(函数)类型。

test.rkt:

#lang typed/racket

(module other-module typed/racket
  (provide f)
  (: f : Integer -> Integer)
  (define (f x)
    (* x 2)))

; g has type Any because dynamic-require returns a value of type Any
(define g (dynamic-require '(submod "test.rkt" other-module) 'f))

;contract violation
;  Attempted to use a higher-order value passed as `Any` in untyped code: #<procedure:f>
;  in: Any
;  contract from: typed-world
;  blaming: cast
;   (assuming the contract is correct)
((cast g (-> Integer Integer)) 3)

有什么方法可以在运行时从in中的其他模块加载和使用函数#lang typed/racket吗?

本·格林曼

一种解决方法是在无类型的模块中进行加载并用于require/typed分配类型:

#lang typed/racket

(module other-module typed/racket
  (provide f)
  (: f : Integer -> Integer)
  (define (f x)
    (* x 2)))

(module another-module racket
  (define g (dynamic-require '(submod "test.rkt" other-module) 'f))
  (provide g))

(require/typed 'another-module
  (g (-> Integer Integer)))

(g 3)
;; 6

但是,如果dynamic-require可以采用目标类型或Typed Racket允许使用非类型化的区域(与相反with-type,那就更好

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章