如何使用WebSharper翻译器?
谁能解释一下如何将 WebSharper 翻译器与 F# 结合使用? 是将 F# 代码本身翻译为 JS 还是使用 F# 编译器?
在第二种情况下,当在源代码中找到 [] 属性时,F# 编译器正在做什么?编译器是否在任何情况下都会生成函数,并在运行时将 JS 构造为编译后的字节码的反射或其他东西?
Could anyone explain how to work WebSharper translator in conjunction with the F#?
Is it translate F# code to JS itself or using F# compiler for it?
In second case, what F# compiler are doing when finds [] attribute in source? Does compiler generate functions in any case and in runtime construct JS as reflection from compiled bytecode or there something other?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我开发 WebSharper。好问题!
编译过程大致如下:
当函数使用
[]
(它是[]
的别名)进行注释时,F# 编译器不会不仅将这些函数编译为 .NET IL,而且还将其语法的表示形式存储在 DLL 元数据中。该表示形式具有Quotations.Expr
类型,并且可以通过反射来恢复。查看Quotations.DerivedPatterns.MethodWithReflectedDefinition
。因此,WebSharper 是一个源到源的转换器,而且非常直接(例如,保留 lambda)。在 WebSharper 2.0 中,我们有一种类似Scheme 的中间语言,但这只是为了帮助优化生成的代码。
I develop WebSharper. Good question!
Roughly the compilation looks like this:
When functions are annotated with
[<JavaScript>]
, which is an alias for[<ReflectedDefinition>]
, the F# compiler does not only compile these functions to .NET IL, but also stores the representation of their syntax in the DLL metadata. This representation has typeQuotations.Expr
and can be recovered by reflection. Have a look atQuotations.DerivedPatterns.MethodWithReflectedDefinition
.WebSharper is therefore a source to source translator, and it is pretty direct (preserves lambdas, for example). In WebSharper 2.0 we have an intermediate Scheme-like language, but that is only there to help optimize the generated code.