racket - Applying cast to dynamically required function in typed racket
问题描述
I'm trying to load and use a function from a different module at run-time. The issue is that dynamic-require
's range, Any
, can't seem to be cast
ed to a more specific (function) type.
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)
Is there any way to load and use a function at run-time from a different module in #lang typed/racket
?
解决方案
One work-around is to do the loading in an untyped module and use require/typed
to assign types:
#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
But yeah, it would be nicer if dynamic-require
could take a target type or Typed Racket allowed untyped regions (the opposite of with-type
).
推荐阅读
- c# - 在尺寸减小时防止图像尺寸增大
- java - 将 Java 对象传递给 Retrofit 无法正确处理
- batch-file - 在批处理文件中使用 WinSCP 从 SFTP 服务器获取具有最新日期的文件
- python-3.x - 我怎样才能让一个方法使用其他方法的变量?
- sql - 如何从相关数据库表中准备 JSON 对象和数组?
- powershell - 带附加的设置 Acl
- amazon-web-services - 在 s3 中上传 400k 文件怎么会有 500 万条消息计数
- javascript - 带有 puppeteer 的 await fs writefile 不会等到文件写入后再继续事件循环
- python - geotif 光栅中的坐标参考系转换
- java - 如果过滤器返回大小为 0 的列表,则抛出异常