简体   繁体   English

可以将F#类型提供程序合并到C#中

[英]Could F# type providers be incorporated in 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. 酷炫的新F#3.0特征类型提供程序可用于弥合F#数据类型或类与XML或WSDL等数据源结构之间的不匹配。 However this mismatch is also a challenge in other .NET languages like C#. 然而,这种不匹配也是其他.NET语言(如C#)的挑战。

I'd like to use the F# 3.0 providers in C# code. 我想在C#代码中使用F#3.0提供程序。 How can I do this, if at all? 如果有的话我怎么能这样做? Further, if we cannot, what would a C# implementation need to be able to use them? 此外,如果我们不能,C#实现需要什么才能使用它们?

I think @kvb gives a good overview of some of the technical difficulties. 我认为@kvb很好地概述了一些技术难题。 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 ). 我认为C#可能会在Roslyn中出现类似的内容,但我怀疑它会像F#一样优雅和流畅地集成(其中类型提供程序实际上是语言功能而不仅仅是工具 )。

To answer your two specific questions: 回答你的两个具体问题:

[How can I] use the F# 3.0 providers in C# code? [我怎么能]在C#代码中使用F#3.0提供程序?

The F# type providers are really only understood by F# compiler, so you'll need to use them from F#. F#类型提供程序实际上只能被F#编译器理解,因此您需要在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. 对于生成类型提供程序(SQL,实体,WSDL,配置文件),您可以从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. 对于擦除类型提供程序,您将无法执行此操作,因为类型实际上并不存在,只有F#才能看到它们。 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#. 因此,最好的选择是在F#中编写处理代码,并将结果作为记录集合或其他易于从C#中使用的类型返回。

What would a C# implementation need to be able to use them? C#实现需要什么才能使用它们?

I could, of course, just say "C# would have to support type providers!", but here are some more thoughts. 当然,我可以说“C#必须支持类型提供商!”,但这里有更多的想法。 Type providers are just .NET assemblies and they do not use any F#-specific types. 类型提供程序只是.NET程序集,它们不使用任何F#特定类型。 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#. 任何.NET语言(包括C#)都可以使用ITypeProvider接口 ,因此如果C#设计者需要,他们可以重用已经为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 + ... ). 因此,将此建议提交给C#用户语音或在其他地方提倡(或说服Mono团队实施此操作!)并且可能会将其添加到C#( vNext + 1 + ... )中。 For now, you'll only get all the benefits in F#. 目前,您只能获得F#的所有好处。

Some aspects of how type providers work are particularly tailored to the needs of F# programmers, but may be less compelling when considering solutions to strongly-typed data access for other languages. 类型提供程序如何工作的某些方面特别适合F#程序员的需求,但在考虑针对其他语言的强类型数据访问的解决方案时可能不那么引人注目。 For instance, a lot of F# programming is done in F# Interactive, and type providers enable this workflow very nicely compared to code generators (which require a language-external mechanism for generating source code files). 例如,很多F#编程都是在F#Interactive中完成的,与代码生成器(需要使用语言外部机制生成源代码文件)相比,类型提供程序可以非常好地启用此工作流。 Since C# programmers are used to slower edit-compile-run cycles, this may be less important in a C# setting. 由于C#程序员习惯于较慢的编辑 - 编译 - 运行周期,因此在C#设置中这可能不那么重要。

From a technical perspective, I suspect that F#'s more pervasive type inference is probably the biggest advantage compared to languages like C#. 从技术角度来看,我怀疑与C#等语言相比,F#更普遍的类型推断可能是最大的优势。 For instance, if I want to wrap some data access logic from a type provider in another type, I can do something like this: 例如,如果我想从另一种类型的类型提供程序中包装一些数据访问逻辑,我可以这样做:

let moviesStartingWith prefix =
    query {
        for movie in MyDataSource.Movies do
        where (movie.Title.StartsWith(prefix) }
    |> Seq.toList

In C#, I'd need to specify the return type (eg List<DataSource.ServiceTypes.Movie> ) which ends up being a chore, and which means that even with IntelliSense, I'm dotting through the set of provided types to generate the signature in addition to dotting through the set of provided values to generate the query. 在C#中,我需要指定返回类型(例如List<DataSource.ServiceTypes.Movie> ),这最终是一件苦差事,这意味着即使使用IntelliSense,我也会点击提供的类型集来生成签名除了点击提供的值集以生成查询。

This applies to areas other than type providers, too, of course, but I think that with some of the nested type hierarchies that are naturally generated by some of the providers this would be especially painful in practice because the type names become extremely long. 当然,这也适用于类型提供者以外的区域,但我认为对于一些由某些提供者自然生成的嵌套类型层次结构,这在实践中尤其痛苦,因为类型名称变得非常长。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM