Welcome to MSDN Blogs Sign in | Join | Help

jaredpar's WebLog

Code, rants and ramblings of a programmer.

Syndication

News

Now Reading

Expert F#

What's a better book to read when learning F#?

Essential WPF

Thus far the best book I've read on WPF. Gets right down to working with WPF and the goals/history.

Purely Functional Data Structures

Reading this book makes me feel like I'm back in college. It will really get your mind going and is best read with a whiteboard handy.

Blog Roll

Eric Lippert
Dustin Campbell
Jon Skeet
Coding Horror
Brian McNamara
Brian Bondy
Hub FS
Full List

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 arguments and create a Func<T> taking no arguments.  This is a great convenience to the user and takes little extra code to implement. 

In addition because we don't expose the EmptyFuture class directly we need to provide a factory method to create it in a non-run state.  Otherwise we are forcing the EmptyFuture to always be created and run in the ThreadPool. 

        public static Future Create(Action action)
        {
            var f = new EmptyFuture(action);
            f.RunInThreadPool();
            return f;
        }

        public static Future Create<TArg1>(Action<TArg1> action, TArg1 arg1)
        {
            return Create(() => action(arg1));
        }

        public static Future<T> Create<T>(Func<T> func)
        {
            var f = new Future<T>(func);
            f.RunInThreadPool();
            return f;
        }

        public static Future<TReturn> Create<TArg1,TReturn>(Func<TArg1, TReturn> func, TArg1 arg1)
        {
            return Create(() => func(arg1));
        }

        public static Future<T> CreateNoRun<T>(Func<T> func)
        {
            return new Future<T>(func);
        }

        public static Future CreateNoRun(Action action)
        {
            return new EmptyFuture(action);
        }

	

Published Saturday, February 23, 2008 8:35 PM by Jared Parsons

Filed under: , ,

Comments

# BioSensorAB &raquo; Factory Methods for Futures @ Saturday, February 23, 2008 8:58 PM

PingBack from http://www.biosensorab.org/2008/02/23/factory-methods-for-futures/

BioSensorAB » Factory Methods for Futures

New Comments to this post are disabled
Page view tracker