A quick COM and F# tip

COM’s got a few quirky nuances. I spent this evening battling a problem in which a C# wrapper had been written around a COM API. This C# wrapper defined an interface called IMediaDet, it also declared a class called MediaDet which should inherit from the interface but doesn’t. Instead the interface and the class shared the same GUID. So I had a C# example which looked like this.

[Guid("65BD0710-24D2-4ff7-9324-ED2E5D3ABAFA"),...]
interface IMediaDet {}

[Guid("65BD0710-24D2-4ff7-9324-ED2E5D3ABAFA"),...]
class MediaDet {}

var mediaDet = new MediaDet() as IMediaDet;

At first you’d think that mediaDet should always be null but it wasn’t, it was actually being assigned due to the underlying COM implementation. Unfortunately in F# something like the above statement isn’t allowed and rightly so, it doesn’t make sense. Thankfully Ross McKinlay was available on Twitter to help and suggested the following

let mediaDet : IMediaDet = unbox(new MediaDet())

The point of the unbox is to avoid the type checker. For the most part though, you should always listen to the compiler and avoid doing things like this as it stops you trying to do something stupid like use COM.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s