如何在我自己的代码中生成类似于FSI的F#类型签名? [英] How to generate the F# type signature similar to FSI in my own code?

查看:76
本文介绍了如何在我自己的代码中生成类似于FSI的F#类型签名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果使用F#交互式外壳程序(FSI),则推断的表达式类型( signature )连同其值一起打印到控制台:

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)

如何在自己的代码中模仿相同的行为,例如以获得推断的类型作为F#表达式的字符串?

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

我不需要动态评估任何F#表达式,这些表达式在编译时是已知的,并且是我(静态)F#代码的一部分.我需要此功能才能为我的F#演示模仿LINQPad中的FSI输出.

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.

推荐答案

使用取消引用

取消引用具有获取类型的F#签名的功能.只需下载最新版本,然后通过LINQPad将引用添加到Unquote.dll,即可进行操作.

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.

如果您有兴趣,可以在FSharpName Type扩展的实现源代码中达到最高水平:

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.

另一种简洁的方法是使用LINQPad的beta 自定义展示器 API嵌入 FsEye 插入LINQPad(FsEye使用与Unquote相同的F#类型签名打印算法).这也非常简单,您只需下载 LINQPad beta ,然后下载并引用FsEye FsEye的最新版本中的.dll,那么您可以执行例如

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.

这篇关于如何在我自己的代码中生成类似于FSI的F#类型签名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆