Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In response to my other answer, Eric Smith correctly notes:</p> <blockquote> <p>"... 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.</p> </blockquote> <p>Thinking about this has led me to a new answer. Try this on for size:</p> <p>Ordinary "instance" methods on structs take, at the CIL level, a "managed pointer" (type <code>&amp;</code>) as a receiver parameter. This is necessary so that instance methods on structs can assign to fields of the struct. See <a href="http://download.microsoft.com/download/7/3/3/733AD403-90B2-4064-A81E-01035A7FE13C/MS%20Partition%20II.pdf" rel="noreferrer">Partition II, Section 13.3</a>.</p> <p>Similarly, instance methods on classes take an "object reference" (type <code>O</code>) as a receiver parameter (the difference being that this is a pointer to the managed heap, and needs to be tracked for GC).</p> <p>Since both CIL <code>&amp;</code>s and <code>O</code>s 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 <code>_target</code> to the first argument of the function.</p> <p>But the scenario we are discussing ruins that. A static extension method taking an <code>int</code> as a first argument requires a CIL argument of type <code>int32</code> (see Partition III, section 1.1.1). <strong>Here is where things go off the rails.</strong> I don't see any reason why it wouldn't be <em>possible</em> 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 <code>_target</code> and pass that as the first argument, but <em>this isn't needed for delegates to classical instance methods on structs, since they expect a pointer anyway</em> 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.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload