Saturday, February 18, 2012

A type extension is F#’s syntax for augmenting an existing type with new members – similar in spirit to C# and VB’s extensions methods. I employ type extensions to make life a bit easier when working with .NET libraries from F#. For example, I might make System.IO.Stream.ReadByte() a bit more natural to use from F# by writing a type extension like so:

type Stream with
    match x.ReadByte() with
    | -1 -> None
    |  b -> Some(byte b)

The function above captures the semantic of the int returned by Stream.ReadByte(), which might be either an unsigned byte or -1 when at the end of the stream. In F#, this semantic is easily represented as an Option and then more easily consumed by other F# code.

So yes, type extensions are dead useful. However, if you’re writing a type extension for a generic type, you’ll need to include all of the type parameters and there constraints. Normally this isn’t a big deal, but I just spent several minutes tearing (more) of my hair out trying to determine the correct incantation to write a type extension for Nullable.

In the documentation, Nullable is declared with two generic constraints: a struct constraint and a default constructor constraint. However, that’s not quite enough to make the F# compiler happy. F# expects a type constraint to System.ValueType as well. So, three constraints are needed to declare a type extension for Nullable.

type Nullable<'a when 'a : struct
                  and 'a : (new : unit -> 'a)
                  and 'a :> System.ValueType> with

    member x.AsOption() =
        match x.HasValue with
        | true  -> Some(x.Value)
        | false -> None

Of course, once you’ve written that, you shouldn’t need to write it again and you can use the extension to handle Nullable a bit more naturally in F#.

match someNullable.AsOption() with
| Some(n) -> printfn "%d" n
| None    -> ()
posted on Saturday, February 18, 2012 3:44:30 PM (Pacific Standard Time, UTC-08:00)  #    Comments [9]

kick it on

These days, I’m using the Sublime Text 2 Beta as my text editor of choice on Windows. I searched around to find the magical incantation to use that would allow Git to use it as my commit message editor and came up empty. It turns out not to be a big deal. Just create a batch file that launches sublime_text.exe with --wait and --new-window arguments. Something like the following:

"C:\Program Files\Sublime Text 2\sublime_text.exe" --wait --new-window "%1"

Then it’s just a matter of setting the batch file as the core editor used by git:

git --config core.editor C:/Scripts/sublime.bat

Update (2/25/2012): The –multiinstance command will do a much better job when launching sublime_text.exe from the command prompt, especially when you already have an instance of sublime_text.exe open. Update your batch file to look like so, and you should be good to go.

"C:\Program Files\Sublime Text 2\sublime_text.exe" --wait --multiinstance "%1"
posted on Saturday, February 18, 2012 12:25:58 PM (Pacific Standard Time, UTC-08:00)  #    Comments [9]

kick it on