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 DotNetKicks.com
Monday, February 27, 2012 3:41:07 PM (Pacific Standard Time, UTC-08:00)
We are gnieneuly remorseful with regard to the bad uk. I am by Mexic, and i am seriously joyful which i discovered your site. It is extremely enlightening, and also amazing. I am truly obtaining a number of pounds challenge these days, and make expectation we can have the very best diet regime, is it possible to point us within the proper course you should?
Thursday, March 01, 2012 4:46:02 PM (Pacific Standard Time, UTC-08:00)
Hi,

we have a toOption converter function in fsharpx (https://github.com/fsharp/fsharpx/blob/master/src/FSharpx.Core/Monad.fs). Maybe it's a good idea to add the extension method for the C# compat part.

Cheers,
Steffen
Tuesday, March 06, 2012 1:28:08 PM (Pacific Standard Time, UTC-08:00)
"... and there constraints"
^ *their* constraints
Richard
Thursday, March 08, 2012 1:33:03 AM (Pacific Standard Time, UTC-08:00)
1.What types of things did the first website ask you to do?The website tells us to read a reversed paragraph in 60 seconds.2.On a scale of 1- 10 how successful were you in completing the tasks? 73.What made completing the task difficult?The letters were reversed, but I made out the words little by little.4.What could the web designer have done to make the site more accessible to you?They made it helpful by informing me of the reversed speech.1.What types of things did the first website ask you to do?It asked me to multi-task.2.On a scale of 1- 10 how successful were you in completing the tasks? 73.What made completing the task difficult?Doing two things at the same time.4.What could the web designer have done to make the site more accessible to you?They could have made it less difficult.

Tuesday, March 13, 2012 6:13:22 PM (Pacific Standard Time, UTC-08:00)
Thanks Alexandra. I can see myself getting a lot of use out of this sweater! Bonus points that it's handmade by Nanny and passed down in the family. xo

Thursday, March 15, 2012 6:26:25 PM (Pacific Standard Time, UTC-08:00)
I wouldn't be able to get rid of it either! I'm not much of a collector, but when it comes to sentimental items, it's impossible for me to part with them. xo

Wednesday, March 21, 2012 10:33:42 PM (Pacific Standard Time, UTC-08:00)
Thanks Heather! I'm obsessed with big, chunky knits. Hopefully one of these days my knitting skills will improve so I can make one myself, but until then I have an incredibly talented grandma to thank. xo

Friday, March 23, 2012 1:26:45 AM (Pacific Standard Time, UTC-08:00)
Bravo les filles pour cette energie Corentin est un copain de mon fils Thomas, ce garcon a une peche d'enfer Je vous dis felicitations A bientotBisous

Saturday, March 24, 2012 5:55:20 PM (Pacific Standard Time, UTC-08:00)
My wife and I would like to say Thank you to Heather for helping us find our lovely house, Your professionalism service was terrific ,We are extremely happy and wish you all the best in your career.Shokran & Mo Sleet

Comments are closed.