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

Mic*_*eod 5 racket typed-racket

我正在尝试在运行时从其他模块加载和使用功能。问题在于,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)
Run Code Online (Sandbox Code Playgroud)

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

Ben*_*man 3

一种解决方法是在无类型模块中进行加载并用于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
Run Code Online (Sandbox Code Playgroud)

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