我正在尝试在运行时从其他模块加载和使用功能。问题是,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] 删除。
我来说两句