When you want to set a date literal in F# (or C#) you create a new DateTime instance:
let today = DateTime(2014,26,4)
If you’re rocking VB.Net you get date literals which are checked at design & compile time:
Dim d = #2014/04/26#
I thought it’d be nice to add design and compile time checked date literals to F#, & throw in code completion for good measure. Enter F# Type Providers. No need to open millions of lines of compiler code, instead simply implement a class to provide the types & voilà:
let today = Date.``2014``.``04``.``26``
As you type the date, you can only enter valid values:
Or you can for example use it to easily find the last Saturday of the month:
The code is up on BitBucket & there’s a Nuget package, it’s pretty simple, less than a 100 lines of code, and no compilers were harmed in the process. Being what it is, this is really just scratching the surface of the power of F# Type Providers, which can give you typed access to data from web services or databases all the way through to other languages.
Build your own Type Provider
Creating your own type providers is easier than you might think, Michael Newton has a great article to get you started: Type Providers From the Ground Up.
Coincidentally the F#unctional Londoners will be hosting a Creating Type Providers Hands On Session with Michael next Thursday at Skills Matter.
Pop along, have fun, and see the kind of things you can build.