繁体   English   中英

在演出的球拍中将施法应用于动态要求的功能

[英]Applying cast to dynamically required function in 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)

有没有办法在运行时从#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相反),那将会更好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM