Anonymous delegate type #8004
Replies: 1 comment 3 replies
-
I don't see why it's onerous to declare a delegate type in these cases, especially since their signature is pretty different from a general purpose The generation of a delegate creates all sorts of additional problems, especially as it is a public name that has to remain consistent between recompilations otherwise it breaks binary compatibility. Thus any such generated naming convention needs to be baked directly into the ECMA/ISO specs. To me, that alone makes it not worth the effort. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
This discussion topic aims to unify the rules on delegate type declarations of parameters and local variables.
A little problem
Today delegate type is a very good mechanism to operate with methods, but sometimes they are still not neat. Consider this example:
Here we have an extension method that can initialize an array via the specified method. However, the method cannot be valid until we define a real delegate type.
Sometimes it is not necessary to define a new type because it may only use once. In addition, it is not generalized because we may sometimes use a delegate type whose signature is same as this delegate type
ArrayInitializer
.If we use function pointers like
delegate*<ref T?, void>
, it may partially solve the problem, but it still leaves something inconvenient:scoped
keywordNotNullAttribute
to the parameterscoped ref T? variable
Due to solving this problem, I suggest C# provide a better way to use delegate types, especially for anonymous delegate types.
Syntax
I suggest use a nearly-same syntax as function pointers to create anonymous delegate types:
Removing token
*
to describe it is an anonymous delegate type, differentiating with function pointers. The same rule with function pointers is, the last "type parameter" of anonymous delegate type declaration is the return type. If the method doesn't require a return type,void
should be explicitly inserted into the last type parameter's position likedelegate<void>
equals todelegate void Action()
.Different with function pointers, we can apply attribute types onto parameters and allow using
scoped
keyword, like the above example.If compiler detects such syntax, it may create a new delegate type or re-use such type having created. The generated type may like this:
Another examples
Other considerations
Synthesized delegate types in API
C# compiler can determine which types are .NET APIs, and which are not. The rules can be simplified like this:
ref
,scoped
,params
, it should be synthesized.Action
andFunc
delegate types group, it shouldn't be synthesized.Complex type declaration
Generic delegate type
Because anonymous delegate types are applied to cases not as declarations, such delegate types may not require type parameters, which is different with real delegate type
In using, we may not apply
<T>
in code so we may not support on this case.Type equivalent on delegate types (
Predicate<T>
vs.Func<T, bool>
)I prefer
delegate<T, bool>
to presentFunc<T, bool>
instead ofPredicate<T>
. Just like the behavior withvar method = Predicate<T>;
.Similar issues
out
,in
, andref
in generic type parameters)Beta Was this translation helpful? Give feedback.
All reactions