Phillip Trelford's Array

POKE 36879,255

Date Types

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:

today

Or you can for example use it to easily find the last Saturday of the month:

saturday

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.

F#unctional LondonersCoincidentally 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.

Comments (1) -

  • genTauro

    4/26/2014 11:36:46 AM |

    Loving it !!!

Pingbacks and trackbacks (1)+

Comments are closed