39

I don't quite get type providers after watching Don Symes's pdc video http://player.microsoftpdc.com/Session/04092962-4ed1-42c6-be07-203d42115274

Do I understand this correctly. You can get ready made type providers for Twitter, Excel...

What if I have a custom Xml structure, do I need to implement my own type provider for that and how is this different from creating my own custom mapper?

terjetyl
  • 9,497
  • 4
  • 54
  • 72

3 Answers3

40

Say you have some arbitrary data entity out in the world. For this example, let's say it's a spreadsheet.

Let's also say you have some way to get/infer schema/metadata for that data - that is, you can know types (e.g. double versus string) and relationships (e.g. this column means 'salary') and metadata (e.g. this sheet is for the June 2009 budget).

Type providers lets you code up a kind of 'shim library' that knows about some kind of data entity (e.g. a spreadsheet) and use that library as part of the compiler/IDE toolchain so that you can write code like

mySpreadsheet.ByRowAndColumn.C4

or something, and get Intellisense (autocompletion) and tooltips (e.g. describing cell C4 as Salary for Bob) and static typing (e.g. have it be a double or a string or whatever it is). Essentially this gives you the tooling affordances of statically-typed object models with the ease-of-use leverage of various dynamic or code-generation systems, with some improvements on both. The 'cost' is that someone has to write the shim library (the 'type provider'), but many such providers are very general (e.g. one that speaks OData or Excel or WMI or whatnot) and so a small handful of type provider libraries makes vast quantities of the world's data available in your programming language with static typing and first-class tooling support.

The architecture is an open compiler, where provider-authors implement a small interface that allows them to inject new names/types into the programming context. A type provider might be just another library you pass to the compiler (a reference in your project, -r-ed), with extra metadata that marks it as a type provider that participates in the compilation/IDE/codegen portions of development.

I don't know exactly what a "custom mapper" is in your xml example to draw a comparison.

Brian
  • 117,631
  • 17
  • 236
  • 300
  • any chance you have info on when type providers will become availible ?Thanks – jlezard Jan 17 '11 at 11:09
  • By custom mapper I meant a library that maps a xml file into a strongly typed object model. I can't see the big difference between such a library and a type providers library – terjetyl Jan 17 '11 at 12:04
  • Because there can be one type provider written for XSD files, and you get strongly typed, discoverable access to any XML file compatible with any XSD, without having to write that mapper or the object model. – Joel Mueller Jan 17 '11 at 16:24
  • Note also that I assume that the 'custom mapper' involves 'code generation' (which comes with its own baggage) and is a specific tool for xml. Whereas type providers are an open mechanism where anyone can author a provider for any kind of data using the same interface/machanism, rather than relying on a fixed set of unique tools for certain domains/data. – Brian Jan 17 '11 at 19:25
9

I understand that this is an old question, but now Type providers are available (as F# 3.0 got released). There is a white paper explaining it too. And we have a code drop from Microsoft that can let you see under the hood.

http://www.infoq.com/news/2012/09/fsharp-type-providers

Roopesh Shenoy
  • 3,389
  • 1
  • 33
  • 50
6

Type providers use F#'s quotations to act as (effectively) compiler plugins that can generate code based on meta-data at compile time.

This allows you to (for example) read in some JSON, or a data base schema, or some XSD or whatever and then generate F# classes to model the domain that meta-data represents.

In terms of creating them, I wrote a few blog posts that might be of interest starting with Type Providers from the Ground Up.

mavnn
  • 9,101
  • 4
  • 34
  • 52
  • Type providers don't really have anything to do with an IL emitter (generative type providers can use one as an implementation detail, but erasing providers don't deal with IL at all). – kvb May 06 '14 at 17:21
  • As far as I can see from ProvidedTypes.fs, even in erasing providers the invocation code you provide for methods and properties as quotations is turned into IL as it is added to the assembly. The types themselves are not, of course. – mavnn May 07 '14 at 09:51
  • I don't have time to look into it further at the moment, but that would surprise me - the raw API is designed around quotations, and the compiler uses patterns like `(|ProvidedCallExpr|_|)` defined in [est.fs](https://github.com/fsharp/fsharp/blob/master/src/fsharp/est.fs) which take quotations from the provider and wrap them, and which are then converted to proper AST nodes in `convertProvidedExpressionToExprAndWitness` in [typreIns.fs](https://github.com/fsharp/fsharp/blob/master/src/fsharp/typrelns.fs). – kvb May 07 '14 at 17:58
  • @kvb I believe you're right about this - I've amended the answer to match. I'd previously misunderstood some of the ProvidedTypes code. – mavnn Sep 02 '14 at 07:58