保持部分应用的函数通用
是否可以部分应用 bprintf 等函数并防止其根据其初始使用而受到限制?
我想做以下事情:
let builder = new System.Text.StringBuilder()
let append = Printf.bprintf builder
append "%i" 10
append "%s" string_value
Is it possible to partially apply a function such as bprintf
and prevent it from being restricted based on its initial use?
I'd like to do the following:
let builder = new System.Text.StringBuilder()
let append = Printf.bprintf builder
append "%i" 10
append "%s" string_value
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
F# 中导致此问题的方面称为“值限制”。您可以看到,如果您仅向 F# Interactive 输入两个
let
声明(以便编译器不会从第一次使用时推断类型):有一个 F# 团队的 Dmitry Lomov 撰写的优秀文章,详细解释了这一点。正如文章所建议的,一种解决方案是添加显式类型参数声明:
这会很好地工作。
The aspect of F# that's causing this is called value restriction. You can see that if you enter just the two
let
declarations to F# Interactive (so that the compiler doesn't infer the type from the first use):There is an excellent article by Dmitry Lomov from the F# team which explains it in detail. As the article suggests, one solution is to add explicit type parameter declaration:
This will work just fine.
您可以添加明确的格式参数
you can add explicit format argument
您遇到了 F# 值限制。
以下是一些解决方法的很好解释:了解 F# 值限制错误
这是一个相当详细的内容 -深度文章解释其背后的原因: 链接
You're encountering the F# value restriction.
Here's a good explanation of some workarounds: Understanding F# Value Restriction Errors
Here's a fairly in-depth article explaining the reasons behind it: Link