views:

934

answers:

2

Extension methods can be assigned to delegates that match their usage on an object, like this:

static class FunnyExtension {
 public static string Double(this string str) { return str + str; }
 public static int Double(this int num) { return num + num; }
}


Func<string> aaMaker = "a".Double;
Func<string, string> doubler = FunnyExtension.Double;

Console.WriteLine(aaMaker());  //Prints "aa"
Console.WriteLine(doubler("b")); //Prints "bb"

If the type they're extending is a value type, it won't work:

Func<int> eightMaker = 4.Double; //Error CS1113: Extension methods 'FunnyExtension.Double(int)' defined on value type 'int' cannot be used to create delegates
Func<int, int> intDoubler = FunnyExtension.Double; //Works

This gives Error CS1113: Extension methods 'FunnyExtension.Double(int)' defined on value type 'int' cannot be used to create delegates.

Why can't they?

+1  A: 

EDIT 2 I don't believe this answer anymore, but I left it here so the thread would still make sense and so that people would see why it isn't right. See my other answer for a different take on the matter.

Original

Because it would require implicitly boxing the value type receiver parameter (because the _target field in the System.Delegate type which holds the the receiver parameter is of type System.Object), which could lead to some strange aliasing behavior if you weren't expecting it.

EDIT

There is something else going on here. I ran this sample program:

class Program
{
    public static int Combine(int a, int b)
    {
        return a + b;
    }

    static void Main(string[] args)
    {
        var combineMethod = typeof(Program).GetMethod("Combine");
        var add4 = Delegate.CreateDelegate(
                              typeof(Converter<int, int>),
                              4,
                              combineMethod) as Converter<int, int>;

        for (int i = 0; i < 10; i++)
        {
            Console.WriteLine(add4(i));
        }
        Console.ReadLine();
    }
}

and got an ArgumentException: "Error binding to target method." at the call to CreateDelegate. I'm not sure why, and because the relevant method is an internalcall method, Reflector isn't much help. The documentation for CreateDelegate also wasn't much help. I'm sure it has something to do with boxing the receiver, maybe someone with knowledge of the Rotor source could help explain why?

Doug McClean
What sort of strange aliasing behavior?
Robert Harvey
What aliasing behavior could it lead to that you wouldn't expect? Since the extension method already takes the value type as a regular parameter, there would be no reason to assume to assume that it wouldn't make a copy.
SLaks
If you call the delegate twice, it would be on the same boxed copy of the structure. I'm not sure if that qualifies as unexpected.
Doug McClean
What difference would that make? The function would still operate on a copy (it's not a ref parameter)
SLaks
I'd say that you are trying to create a delegate with the receive type Convert<int,int> but with a method that belongs to the type Program. Which is why you in your example get the exception
Rune FS
@runefs, no, I'm trying to create a delegate of type `Converter<int,int>` with a receiver of type int. If you change all the `int`s to `string`s and the literal `4` to `foo` you'll find that the program is well typed and runs without error.
Doug McClean
And apparently I can't format or edit this comment, so ignore the backticks above.
Doug McClean
"... because it would require implicitly boxing the receiver type parameter ...". Which is what happens anyway, if you do something like this: Func<string> f = 5.ToString;Which is perfectly legal.
Eric Smith
Eric is right. How's this answer instead: http://stackoverflow.com/questions/1016033/extension-methods-defined-on-value-types-cannot-be-used-to-create-delegates-why/1016763#1016763
Doug McClean
+8  A: 

In response to my other answer, Eric Smith correctly notes:

"... because it would require implicitly boxing the receiver type parameter ...". Which is what happens anyway, if you do something like this: Func f = 5.ToString; Which is perfectly legal.

Thinking about this has led me to a new answer. Try this on for size:

Ordinary "instance" methods on structs take, at the CIL level, a "managed pointer" (type &) as a receiver parameter. This is necessary so that instance methods on structs can assign to fields of the struct. See Partition II, Section 13.3.

Similarly, instance methods on classes take an "object reference" (type O) as a receiver parameter (the difference being that this is a pointer to the managed heap, and needs to be tracked for GC).

Since both CIL &s and Os can be (and are) implemented by pointers, everything is hunky-dory for the delegate implementation. Regardless of whether a delegate captures a static method, a class instance method, or a struct instance method, all it needs to do is pass the pointer to its _target to the first argument of the function.

But the scenario we are discussing ruins that. A static extension method taking an int as a first argument requires a CIL argument of type int32 (see Partition III, section 1.1.1). Here is where things go off the rails. I don't see any reason why it wouldn't be possible for the implementation of delegates to realize that this was happening (for example, by inspecting the metadata associated with the MethodInfo being captured) and emit a thunk that would unbox the _target and pass that as the first argument, but this isn't needed for delegates to classical instance methods on structs, since they expect a pointer anyway and doesn't appear (judging by the example in my earlier incorrect answer) to be implemented. Obviously the specific value type in question would control the exact nature of the required thunk.

Unless I am missing a more fundamental obstacle to implementation (I could imagine that it would pose problems for the verifier, for example), it seems like a reasonable case could be made for extending the runtime to support this case, but all the signs are pointing towards this being a limitation of the runtime and not of the C# compiler per se.

Doug McClean
Doug, your analysis is excellent. As we discussed by email, my collegue Sreekar has made some notes in his blog http://blogs.msdn.com/sreekarc/archive/2009/06/25/why-can-t-extension-methods-on-value-type-be-curried.aspx and I've put some notes on how this problem shows up with "reflection currying" in my blog. http://blogs.msdn.com/ericlippert/archive/2009/06/25/mmm-curry.aspx
Eric Lippert
Huge thanks to Eric and Sreekar for taking the time to dig into this and explain it for everyone!
Doug McClean