What is the advantage of F# Type Providers over traditional 'type providers'?

StackOverflow https://stackoverflow.com/questions/19403555

  •  30-06-2022
  •  | 
  •  

Вопрос

From the MSDN page on F# Type Providers:

An F# type provider is a component that provides types, properties, and methods for use in your program.

So it is like a .NET class library? What is the difference? And:

Writing these types manually is very time-consuming and difficult to maintain.

Does a Type Provider write itself automatically? There is more:

Similarly, a type provider for WSDL web services will provide the types, properties, and methods you need to work directly with any WSDL web service.

There are utilities for generating types from a WSDL URL, again what is the advantage provided by Type Providers here?

My first thoughts were F# Type Providers provide types at runtime like .NET remoting but that does not seem to be the case. What are the advantages of using them?

Это было полезно?

Решение

In many ways, code generation is a natural comparison for type providers. However, type providers have several desirable properties that code generation lacks:

  • Type providers can be used in F# scripts without ever having to context switch. With a code generator, you'd have to invoke the generator, reference the code, etc. With a type provider you reference the type provider assembly (which is just like referencing any other F#/.NET assembly) and then use the provided types right away. This is really a game changer for interactive scripting.
  • As Gustavo mentions, erased types allow type providers to handle situations where traditional code generation would generate too much code (e.g. Freebase has thousands of types, which is no problem for a type provider).
  • Type providers can support invalidation, so that if a data source changes the compiler will immediately recheck the file.
  • Likewise, with a code generator it's possible for the generated code to get out of sync with the data source; type providers can prevent this problem from occurring, inspecting the data source each time your program is compiled (though many type providers also provide the option of using a cached schema for convenience).
  • Type providers are arguably easier to implement, though it probably depends on the scenario and the author's background.

Другие советы

You can generate types from a WSDL or from a DB using a code generation tool, as the ones integrated into Visual Studio. Type providers do basicly the same, but integrate that process directly in the compilation. This way you don't need to worry about regenerating the types when the the schema changes.

Additionaly, type providers support doing this with erased types, which are "virtual" types that don't really exist. This means that instead of generating 500 types and a big assembly, only what is actually used is generated, which means smaller assemblies and support for importing huge and recursive schemas, like Freebase

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top