Clarifying on some points in the static-delegates proposal.

This commit is contained in:
Tanner Gooding 2017-02-16 08:02:34 -08:00
Родитель 6b8bd95f8b
Коммит e23c9e0ebd
1 изменённых файлов: 9 добавлений и 4 удалений

Просмотреть файл

@ -33,15 +33,20 @@ One could additionally attribute the declaration with something similar to `Syst
The declaration would get translated into an internal representation by the compiler that is similar to the following
```C#
struct <name>
struct <Func>e__StaticDelegate
{
IntPtr pFunction;
static int Func();
static int WellKnownCompilerName();
}
```
That is to say, it is internally represented by a struct that has a single member of type `IntPtr` (such a struct is blittable and does not incur any heap allocations). The member contains the address of the function that is to be the callback. Additionally, the type declares a method matching the method signature of the callback.
That is to say, it is internally represented by a struct that has a single member of type `IntPtr` (such a struct is blittable and does not incur any heap allocations):
* The member contains the address of the function that is to be the callback.
* The type declares a method matching the method signature of the callback.
* The name of the struct should not be user-constructable (as we do with other internally generated backing structures).
* For example: fixed size buffers generate a struct with a name in the format of `<name>e__FixedBuffer` (`<` and `>` are part of the identifier and make the identifier not constructable in C#, but still useable in IL).
* The name of the method declaration should be a well known name used across all static delegate types (this allows the compiler to know the name to look for when determining the signature).
The value of the static delegate can only be bound to a static method that matches the signature of the callback.
@ -49,11 +54,11 @@ Chaining callbacks together is not supported.
Invocation of the callback would be implemented by the `calli` instruction.
## Drawbacks
[drawbacks]: #drawbacks
Static Delegates would not work with existing APIs that use regular delegates (one would need to wrap said static delegate in a regular delegate of the same signature).
* Given that `System.Delegate` is represented internally as a set of `object` and `IntPtr` fields (http://source.dot.net/#System.Private.CoreLib/src/System/Delegate.cs), it would be possible to allow implicit conversion of a static delegate to a `System.Delegate` that has a matching method signature. It would also be possible to provide an explicit conversion in the opposite direction, provided the `System.Delegate` conformed to all the requirements of being a static delegate.
Additional work would be needed to make Static Delegate readily usable in the core framework.