难道F#类型提供在C#中被纳入类型

2023-09-03 00:03:14 作者:哈努卡前的犹太人

很酷的新F#3.0功能型供应商可用于弥补F#的数据类型或类和数据源的结构之间的不匹配,如XML和WSDL。然而,这不匹配也是其他.NET语言如C#是一个挑战。

The cool new F# 3.0 feature type providers can be used to bridge the mismatch between F# data types or classes and data source structures like XML or WSDL. However this mismatch is also a challenge in other .NET languages like C#.

我想使用F#3.0供应商在C#code。我该怎么做,如果在所有?此外,如果我们做不到,你会C#实现需要能够使用它们?

I'd like to use the F# 3.0 providers in C# code. How can I do this, if at all? Further, if we cannot, what would a C# implementation need to be able to use them?

推荐答案

我觉得@kvb给出了一些技术上的困难一个很好的概述。我同意的的类型推断的是有问题 - 你会基本上仅限于使用提供生成的类型在本地,同样的匿名类型的。我认为C#可能出现在罗斯林类似的东西,但我怀疑这将是作为优雅和顺利地集成在F#(其中类型提供实际的语言功能的,而不是仅仅的工具)

I think @kvb gives a good overview of some of the technical difficulties. I agree type inference would be problematic - you would be basically limited to using provider generated types locally, similarly to anonymous types. I think C# might come with something similar in Roslyn, but I doubt it will be as elegantly and smoothly integrated as in F# (where type providers are actually language feature and not just tool).

要回答你的两个具体问题:

To answer your two specific questions:

[我如何]使用F#3.0供应商在C#code?

[How can I] use the F# 3.0 providers in C# code?

F#的类型提供实际上只由F#编译器的理解,所以你需要使用它们的F#。对于生成的类型提供(SQL,实体,WSDL,配置文件),你可以参考一下从F#的供应商,并使用生成的类型从C#项目。

The F# type providers are really only understood by F# compiler, so you'll need to use them from F#. For generative type providers (SQL, Entities, WSDL, config files), you can reference the provider from F# and use the generated types from C# projects.

有关的擦除的类型提供你不会是能够做到这一点,因为类型不真的存在,只有F#可以看到它们。所以最好的办法是写你的处理code F#中,并返回结果的记录或其他类型的集合,很容易从C#消耗。

For erasing type providers you won't be able to do this, because the types do not really exist and only F# can see them. So the best option is to write your processing code in F# and return results as collections of records or other types that are easily consumed from C#.

什么是C#实现需要能够使用它们?

What would a C# implementation need to be able to use them?

我可以,当然,只是说C#必须支持类型提供!,但这里有一些想法。类型供应商只是.NET程序集和他们不使用任何F#的特异类型。该 ITypeProvider 接口可以通过消耗任何.NET语言,包括C#,所以如果C#设计者希望,他们可以重复使用所有已建成为F#大供应商。

I could, of course, just say "C# would have to support type providers!", but here are some more thoughts. Type providers are just .NET assemblies and they do not use any F#-specific types. The ITypeProvider interface could be consumed by any .NET language including C#, so if C# designers wanted, they could reuse all the great providers already built for F#.

所以,提出这个建议的 C#用户的语音或其他地方鼓吹它(或说服Mono团队来实现这个!),或许它会在C#中添加( vNext + 1 + ... )。现在,你得到的就只是在F#中的所有好处。

So, submit this suggestion to the C# user voice or advocate it elsewhere (or convince the Mono team to implement this!) and perhaps it will be added in C# (vNext + 1 + ...). For now, you'll only get all the benefits in F#.