Roundcrisis

home about talks rss feed  rss

Using Type providers

27 Feb 2016

I wrote a little bit about type providers here but I haven’t in a while and the time has come to check them out again.

The problem

Let’s say that I want to use the Giphy API, which gives back data in json format including a (generally funny) gif. From F# we could take advantage of type providers to avoid a bunch of boiler plate code.

A concrete solution

  1. On an F# script file add a reference to Fsharp.Data nuget package.
  2. In the Giphy website you can find the base url and the key to make a request.
  3. Use the following code:

type GiphyTP = JsonProvider<"http://api.giphy.com/v1/gifs/search?q=monkey+cat&rating=pg-13&api_key=dc6zaTOxFJmzC">
let baseUrl = "http://api.giphy.com/v1/gifs/search"
let key = // Since Giphy might change the key from time to time, go fetch it off their site

let searchGif searchString =
  let searchTerm = String.map(fun c ->  if (c = ' ' )then '+' else c ) searchString
  let query = ["api_key", key; "q", searchTerm]
  let request = Http.RequestString (baseUrl,  query)
  let giphy = GiphyTP.Parse(request)
  giphy.Data
  |> Array.map( fun x -> Uri(x.Images.DownsizedMedium.Url))

One of the results is:

otter

Which is pretty cool, What happened?

tp

If you look at that picture in detail you might wonder, How is it that given a json document, the compiler can give me type information (i.e. Frames, Height are int, Images is a collection, etc), well, that is exactly what the type provider is doing, infering the type at runtime so that you don’t have to. For each element it will try to infer first primitive types and then by combining them , more complex types.

As mentioned in the previous post, the type provider will give you a signature, on demand, as needed by the compiler. At the same time if we decompile that code you will see something like this.

dotpeek

and that is because in the JSon type provider, the types are erased. A type provider does not necessarily contain any types itself; rather, it is a component for generating descriptions of types, methods and their implementations.

Next, let’s see what do type providers look like in other languages.

Resources

comments powered by Disqus