Skip to content

Instantly share code, notes, and snippets.

@JamesTryand
Created April 30, 2012 14:29
Show Gist options
  • Select an option

  • Save JamesTryand/2558802 to your computer and use it in GitHub Desktop.

Select an option

Save JamesTryand/2558802 to your computer and use it in GitHub Desktop.
Helper class to infer type from lambdas in C#
/// <summary>
/// These classes are helper classes to help type inference for lambdas.
/// <example>var f = () => 4; // will not compile,
/// var f = Infer.Func(() => 4); // will compile.</example>
/// <see cref="http://blogs.msdn.com/b/jaredpar/archive/2007/12/14/c-lambda-type-inference.aspx"/>
///
/// </summary>
public static class Infer
{
public static Func<A> Func<A>(Func<A> f)
{
return f;
}
public static Func<A, B> Func<A, B>(Func<A, B> f)
{
return f;
}
public static Func<A, B, C> Func<A, B, C>(Func<A, B, C> f)
{
return f;
}
public static Func<A, B, C, D> Func<A, B, C, D>(Func<A, B, C, D> f)
{
return f;
}
public static Func<A, B, C, D, E> Func<A, B, C, D, E>(Func<A, B, C, D, E> f)
{
return f;
}
public static Action<A> Action<A>(Action<A> f)
{
return f;
}
public static Action<A, B> Action<A, B>(Action<A, B> f)
{
return f;
}
public static Action<A, B, C> Action<A, B, C>(Action<A, B, C> f)
{
return f;
}
public static Action<A, B, C, D> Action<A, B, C, D>(Action<A, B, C, D> f)
{
return f;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment