Design Guidelines: Provide type inference friendly Create function for generic objects

Really this guideline is a bit longer but putting it all in a blog title seemed a bit too much.  The full guideline should read: "If a generic class constructor arguments contain types of all generic parameters, provide a static method named Create on a static class of the same class name as the generic…

3

Have an IComparer<T> but need an IComparable<T>?

Previously we discussed the opposite problem.  This is a lesser but often more frustrating problem because there is no, AFAIK, built in solution for the BCL.  However it’s problem that can be solved once and reused with a generic solution. IComparable<T> has all of the methods necessary implement IComparer<T>.  To work around this we’ll create…


Have a IComparable(Of T) but need an IComparer(Of T)?

IComparable(Of T) is an interface saying "I can compare myself to other objects of the same type".  And IComparer(Of T) is an interface saying "I can compare two objects of this type.".  Often API’s which need to perform comparisons will take an instance of IComparer(Of T) instead of IComparable(Of T).  Doing the opposite is limiting…


Missing API: List(Of T).BinaryInsert

One API that seems to be missing from List(Of T) is a BinaryInsert method.  Especially since there is already a BinarySearch method. Binary insert is a method for inserting a value into an already sorted list.  Since the list is already sorted we can do a binary search to find the appropriate place to insert. …

3

API Problems: CComObject::CreateInstance

CComObject::CreateInstance is a light weight method for creating instances of COM objects in your code.  Unfortunately the design of the API makes it easy to introduce subtle errors into your code.  The two problems are it encourages manually ref counting and the object initially has a ref count of 0.  This means you must remember…

2

SynchronizationContext and Higher Order Functions

It’s often useful to ensure that actions occur on specific threads, in particular event handlers.  Take Windows Forms for instance where all operations on a Control must occur on the thread it was created on.  Typically this is not a problem since WinForms respond to events such as Click, Move, etc…  These events are sourced…

1

Factory Methods for Futures

Like most generic classes, I prefer to create Future instances through static factory methods which allows me to take maximum advantage of type inference.  In addition to the 2 straight forward declaration of Func<T> and Action, the methods will include overloads which take in Func<T> with varying numbers of arguments.  The overloads will cury the…

1

Multiple paths to IUnknown

ATL has a lot of great tools for COM programming and CComPtr is a good example.  It’s a smart pointer class which manages the reference count of an underlying COM object. One of it’s limitations though is it will only work properly when the inheritance chain for a class has only one path to IUnknown. …


Building a Future which returns no Value

In addition to Future<T> there is also the concept of Futures that don’t return any values.  Instead the perform the operation and return.  Because there is no additional data to pass between the threads building an Empty Future is fairly straight forward. The biggest decision is how to declare it.  EmptyFuture exposes no new accessible…

1

Push Enumerators

If you read Jon Skeet’s blog you’ll notice he’s been playing around lately with “push” style enumerators.  Push enumerators are the concept of “we’ll tell you when we’re ready”.  This is different from IEnumerator<T> which is more of a pull; “ask me if I have more data model”. His latest idea is an interface based…

1