How to generate the F# type signature similar to FSI in my own code?

主宰稳场 提交于 2019-12-18 15:09:30

问题


If one uses the F# Interactive Shell (FSI), the inferred expression type (signature) is printed to the console along with its value:

val it : int * string * float = (42, "Hello F#", 42.0)

How can I mimick the same behaviour in my own code, e.g. to get the inferred types as string for a F# expression?

I don't need to dynamically evaluate any F# expressions, the expressions are known in compile time and are part of my (static) F# code. I need this feature to be able to mimick the FSI output in LINQPad for my F# demos.


回答1:


Using Unquote

Unquote has a facility for getting the F# signature of a type. Simply download the latest release and add a reference through LINQPad to Unquote.dll, then you can do e.g.

If you're interested, you can peak at the source code for the implementation of the FSharpName Type extension: http://code.google.com/p/unquote/source/browse/tags/2.1.0/Unquote/ExtraReflection.fs#54.

Using FsEye

Another neat approach would be to use LINQPad's beta Custom Visualizer API to embed FsEye into LINQPad (FsEye uses the same F# type signature printing algorithm as Unquote). This is also very simple, all you need to do is download LINQPad beta, download and reference FsEye.dll from the latest release of FsEye, then you can do e.g.




回答2:


If you look at the F# compiler code and see how the --sig option is handled by the compiler I think that will get you what you're looking for. More about the --sig option and signatures here:

Signatures (F#)



来源:https://stackoverflow.com/questions/9908052/how-to-generate-the-f-type-signature-similar-to-fsi-in-my-own-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!