Type providers are one of F#'s most interesting features, but using them can often feel a bit like magic. By building your own type provider, you can gain a better understanding of how type providers work.
Type providers are one of F#'s most interesting features, but using them can often feel a bit like magic. By building your own type provider, you can gain a better understanding of how type providers work. In this course, you'll build a simple type provider for reading ID3 frames from an MP3 file using the Type Provider Starter Pack. Along the way, we'll discuss the type erasure and explore the role of code quotations in type provider development.
Introduction to Type Providers If you have been working with F# for any amount of time chances are good that you've probably at least stumbled upon Type Providers and heard how amazing they are. Whether you've only seen them mentioned in documentation or forums, have used them and want to learn more about how they work or have some data you'd like to expose through a custom type provider this is the course for you. I'm Dave Fancher and I'll be your guide as you delve into building a custom type provider. Before we begin there are some things you should know. First, this course assumes that you possess at least a basic working knowledge of F# and functional programming. If this is your first exposure to F# or you need a refresher you should consider watching Oliver Sturm's Introduction to F# course here on Pluralsight. Also, each of the demos will be developed with Visual Studio 2013. I will be using the ultimate edition, but other editions should suffice. Our work in Visual Studio will be focused on type provider development and not Visual Studio itself, so only a basic working knowledge of Visual Studio is expected. For a comprehensive look at Visual Studio 2013 consider watching both parts of Kate Gregory's introduction to Visual Studio 2013 course also here on Pluralsight.
Setting the Stage Now that we've defined type providers and seen a few in action we're just about ready to begin constructing our own. Throughout this module we'll step in and out of Visual Studio to lay the foundation of our custom type provider, which will provide a type that represents the data contained within an mp3 file's ID3 tag. This will make it easy to access things like album art, artist name, track name, and so on in a type-safe manner. Before we begin construction we first need to discuss a few criteria to consider before building a custom type provider. From there I'll then briefly introduce the type provider sample pack, which will help us get our custom type provider up and running quickly. Finally, we'll take a look at the ID3 V2 format to get a better understanding of how the type provider works from end to end.
Providing a Type Now that we've built the foundation for the ID3 type provider we're ready to start implementing its functionality. In this module we'll see how the Type Provider Starter Pack simplifies implementing the ITypeProvider interface and how to provide a type. Along the way we'll stop to look at type erasure and code quotations as they relate to type provider development.
Providing Methods and Properties Coming into this module we have everything in place to create instances of our ProvidedType. That type doesn't yet give us any visibility into the underlying ID3 tag, so in this module we'll fix that by defining a ProvidedMethod and some properties. These will let us easily access the frames from the consuming application. As we progress we'll also develop a simple consuming application and finally see the ID3TypeProvider in action.
Type Erasure Revisited In module 3 we discussed type erasure and what it means for a provided type to be erased during compilation. At that time we left the discussion at a purely conceptual level, without diving into any concrete examples. Now that we have an operational type provider and test client we can look at what the compiler produces whenever we reference the provided type.