[英]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.