Wednesday, July 11, 2012

Type Provider Project Template

I believe a beard well lathered is half shaved. If I have to do the type provider every now and often, I'd better get this process automated. I already have the code snippet support add basic building blocks for type provider class, but i still need to add the API files and often need to use Alt+Arrow Up/Down to adjust the file orders in the project.

I need to find a way to get this sorted out automatically. The Type Provider template is a solution. It can generate the basic project skeleton with API source file, test script, and the backbone type provider source code. The following screen shot shows how it works when add the package. 




The type provider template will sync with the code in the F# 3.0 sample pack


3 comments:

Art said...

F# type providers for C++ templates?

Thanks Tao Lui

Tao Liu said...

The C++ does not support type provider. Only F# has support type provider concept, so there is no C++ template.

Art said...

F# type providers, generics and C++ templates

AntonioCisternino from
by AntonioCisternino

F#, as any other .NET language, relies on the ability of the Common Language Runtime of representing generic types (i.e. types with parameters such as List).

.NET generics have a very different design from C++ templates even if they serve the same purpose: templates are a sophisticated form of macro expansion of abstract syntax trees, meaning essentially that the compiler is able to type check a template only after its instantiation with a concrete type (i.e. List). CLR generics, on the other hand, have been designed to be type-checked at definition time (i.e. when with List has not given a value for T), a more cleaner design especially in an environment where compilation and loading are interwined in a continuous process.

The cleaner design, however, comes at a price: popular programming patterns for generic programming are more difficult to implement. In particular, duck typing (i.e. unrelated types sporting the same set of operations can be used for instantiate a C++ template), partial specialization (i.e. a special implementation for a specific generic type instantiation such as List), and Turing equivalence for carrying computations at compile time (ok, I reckon that this is peculiar to C++...).

Type providers may fill the gap, only for F# Language though. The reason is that a type provider is a module loaded by the compiler that is capable of carrying any computation in order to generate a set of types that a program may use. This ability allows to address naturally the partial specialization and the Turing equivalence issues mentioned above. As for the duck typing, it can be approximated using a technique based on erased types: it is not equivalent to the macroexpansion approach taken by C++, but it allows to return a particular type inheriting from whatever type is required and generating the appropriate type after checking at compile time for the required set of operations. In this way an unconstrained generic type can be used by any type because of new support for variance and contravariance in generics.