F# And Type Providers

You’re going to think I’m jumping on the F# bandwagon as well. Well kind of but that’s not the point of this post. The thing is, people ask me all the time: Why should I even care about F#? I have my own C# language that I love, why go through the burden of learning another language, and more importantly, another way of thinking, as functional programming is not only about the language but more conceptual way of thinking. At the end of the day, you may say, these are all languages on top of .NET stack and compile into IL so there’s really not THAT much difference, is there?? Just show me one thing that I can do with F# and I can’t do with C#?

Turns out, there are a few things that you can’t do with C#. Among the things that is not possible to do - at least not at this point in time - is what Type Provider gives you. Let’s quickly see what they are.

What is a type provider

Essentially, type provider provide type information (wasn’t that obvious?) to the compiler as you write your code. If you’ve ever had a joy of working with WebServices (WSDLs), XML or CSV files and things of that nature you probably had to write or generate some sort of class hierarchy to represent that structure and the use that to do what you need. A type provider will do all that for you just by having a look at a schema - that being a sample XML / CSV file with the same structure or the actual WSDL over the internet - and it will generate all the types you need to work with it. Best of all, it is all statically typed, which means once your schema is changed your code automatically won’t compile. This is unlike the WSDL code generation approach that you’d have to go with, where as you’d have to regenerate the code manually to get compilation errors.

Where do I use it

As said, a simple usage would be generating a wrapper for your XML / CSV files to work with. But there are even more interesting things you can do. When it comes down to interconnected data structures like DBPedia, WorldBank and Freebase, generating and working with a huge API is probably not the best way, but with Type Providers for all of these rich information data sources, you can work with them as if there are statically type APIs for them.

Where do I go from here

This was barely even a definition of Type Providers. If you want to learn a bit more and see some samples, you can view my talk about F# Type Providers from at the Sydney F# User Group that is now online and you can watch it here.