将强制转换应用于类型化球拍中动态需要的功能
Applying cast to dynamically required function in typed racket
我正在尝试在 运行 时加载和使用来自不同模块的函数。问题是 dynamic-require
的范围 Any
似乎无法 cast
ed 为更具体的(函数)类型。
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
中的不同模块加载和使用函数?
一个work-around是在untyped模块中加载,使用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
相反),那就更好了。
我正在尝试在 运行 时加载和使用来自不同模块的函数。问题是 dynamic-require
的范围 Any
似乎无法 cast
ed 为更具体的(函数)类型。
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
中的不同模块加载和使用函数?
一个work-around是在untyped模块中加载,使用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
相反),那就更好了。