Resharper – Using custom patterns to catch subtle bugs with OfType<T>

One of the pitfalls with LINQ which I have fallen into on multiple occasions is around the use of the OfType<T> extension method with dictionaries.

Either through carelessness or changes to the underlying variable (a class property being changed from list to dictionary for instance) I have ended up using OfType<…> on a dictionary which compiles fine but unless I’m specifically looking for KeyValuePair<TKey, TValue>, it’s not going to behave as I’d expected…

image

Curiously, however, with most other IEnumerable<T> types Resharper is able to spot the danger signs and give me a warning:

image

Turns out Resharper has a really nice feature – Custom Patterns – which I hadn’t been aware of until recently, that allows you to specify your own patterns which can then trigger suggestions/warnings/errors etc. when matched against elements of your code. I can now create a custom pattern to find places where I have tried to use the OfType<T> extension method against a dictionary, like the following:

image

image

Now, if we go back to the source code, you can see that Resharper has now picked up on my mistake using my pattern:

image

and since we specified a Replace pattern above we can even use the Resharper helper to quickly fix our code!

image

F# equivalent of C#’s Object Initialization syntax

In C#, you can use the object/collection initialization syntax like this:

image

The F# equivalent of object initialization is done like this:

image

As for collection initialization, you have a far more diverse range of tools available to you, for example:

image

You can also create slices of an existing array:

image

You can even add your own multi-dimensional indexers to support similar behaviours in your type too!

Update 2012/01/11:

As Robert Pickering mentioned in the comments, slicing is not limited to arrays, it works with other collection types too (as long as a GetSlice extension method is defined on the type, for more information, see here). For instance, you can use slicing on a string like this:

image

With 2D arrays, you can also use a multi-dimensional slicer too:

image

F# – Converting to and from Units of Measure

If you’re reading this post, you probably know about F#’s Units of Measure already, it’s very useful when working with real-world units and adds extra safety to code that needs to work with and convert from one unit to another.

Here’s a quick snippet that shows you how to define and use units-of-measure:

image

This code outputs the following, note the units associated with the float values:

image

As you can see, units of measure can also be compounded by multiplication or division!

If you have a function that requires a int<m>, you won’t be able to call the function with a normal int, hence providing you with extra protection to ensure the correctness of your application because the unit of a numeric value is now a formal contract between a function and its caller:

image

Having said that, there are cases where you want to be able to convert between an int and an int<m>. For instance, to provide better interoperability with other .Net languages, as units-of-measure only exists in F# there’s no way to create a numeric value with units-of-measure in C# (that I’m aware of anyway).

To convert from int<m> to int (or any other numeric type) is easy, just do a straight cast:

image

Going the other way is slightly more tricky, you can’t use int<m> to cast an int to an int<m>, but you can either multiply the value with 1<m> or use the Int32WithMeasure method on the LanguagePrimitives core module:

image

F# – Define empty class, struct or interface types

In C#, you define an empty class, struct, or interface like this:

image

So how do you define an empty type in F#?

Well, whenever you define a new class in F#, the compiler infers the class and end tokens at the beginning and end of the class’s definition, as you can see from below:

image

So the easiest way to define an empty type is to specify the class and end tokens explicitly:

image

Running these few lines of code into the F# interactive outputs the following:

image

For class types, you also have other alternative techniques, such as:

image

F# – From string to byte array and back

There’re a number of built-in literal types in F#, and one of the cool things you can do is to get the byte or byte array representation of a char or string using the ‘B’ suffix, the result is the same as Encoding.ASCII.GetBytes:

image

Pretty cool, eh?