-
Notifications
You must be signed in to change notification settings - Fork 4.1k
[proposal] Support anonymous delegate type #16488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Related #3990 |
As an aside, this suggestion has triggered a thought: what's the plan for generic delegates and ref returns? If I have eg, ref T F<T>(T p) => ... I assume I can't assign this to a |
@DavidArno I guess it's the same issue when passing something by |
@DavidArno That's why I was proposing transpile to generate delegate automatically to map what we need Something such delegate void _ActionRef<P>(ref P param);
delegate R _FuncRef<R,P>(ref P param);
delegate ref R _RefFunc<R>();
delegate ref R _RefFuncRef<R,P>(ref P param); and alike to map automatically |
@eyalsk One way to make it work is to generate all possible combination (at least 8 parameter, same as Action<> and Func<>) hidden in For something weirder than 8 parameter could be unsupported |
Thanks. I'll raise a new issue about that topic therefore as it isn't really related to this one. |
Do you realize thats total of 2*((3^1)+(3^2)+(3^3)+...+(3^8)) combinations? :-) |
@zippec Yeah but unfortunately atm there's no other way to support it without adding these types, there's also no notion of variadic functions. |
@zippec Well, at first I think it is 8^3 which is around 512 Still Which, if we just support 4 parameter it then decrease to around 300 |
@Thaina What we really need is variadic generics before adding more of these types. |
I largely don't think this is needed. If you need a "complex" delegate that can't be covered by the For local-only delegate instances that problem goes away, but you still have the issue stipulated in #3990 where the lack of signature equivalence means that you end up with a new delegate type which would be incompatible with any parameter you'd wish to pass it to. |
@eyalsk Anonymous delegates specifically. Note that I like the idea of inferred and/or simpler delegate type syntax, including |
@HaloFour Sorry for deleting my comment after your edit.
I'd really love to have these two features that is a delegate type syntax and more type inference for both delegates and generics. p.s. Couldn't post because GitHub was down or something.. |
How do you propose the compiler infers the type of the latter delegate? Until we get generic operators, extension operators and concepts it won't be able to infer if it's a delegate on strings, ints or custom structs. |
To be honest I've not thought it through. The options might be that C# go the F#/Haskell route where the lambda arguments are effectively "generic" (not in the CLR sense) and that the compiler will collapse the definition into an implementation based on use (possibly more than once): var add = (x, y) => x + y;
var i = add(2, 3); // produces 5
var s = add("Hello ", "World"); // produces "Hello World" Or the compiler could require that the lambda arguments include type information: var add = (int x, int y) => x + y;
int result = add(2, 3); Consider that very uncooked spaghetti, though. |
@HaloFour (I have no idea why I mix you two up) IIRC, Haskell actually infers the type class that provides |
@HaloFour I really wish we would have that feature you mention. It just need to require CLR support so I just think a way to make it possible without If CLR can just support that it would be the best. To be honest I would like to write a function type just where it need without naming it. Such as function parameter, if it would use only in one place void LoadSomething(string where,void(Stream) callBack)
{
} I think delegate is legacy from when the programming trend is to force everything to has type. But now we start moving to anonymous direction (from anonymous object in C# 4 to Tuple in C# 7) maybe we could add anonymous function as a superclass of delegate, like tuple? |
I believe Delegates were specifically designed to provide structural equivalence and thereby provide needed flexibility. Anders Hejlsberg has spoken about this before, using event handlers as a motivating example where structural equality is desirable and matches programmer intuition.
Anonymous types, added in C# 3.0, are very strongly typed. The compiler shares the underlying implementations of equivalent anonymous types between methods within an assembly. But that is just an implementation detail, the real issue, as others have said, as is cross assembly binding.
All delegate types do derive from System.Delegate, but the problem is that this type is lossy.
The equivalent F# is actually an error. It is one of the things I find counter-intuitive about automatic generalization. It creates the type eagerly on first use, but only once (I'm not sure what Haskell does).
This seems completely reasonable and matches how lambdas currently behave when they are target typed by generics so it would imply no change in the inference of the actual signature. |
And that's what I always would like it to be in everything. I wish anonymous anything would be strong type. Just that it can be strong without name of the type. The same for anonymous delegate too. I just don't want to name it but it should be compiled as strong type That's why I talk about superclass, in addition of |
Closing this out. We're doing all language design now at dotnet/csharplang. If you're still interested in this idea let us know and we can migrate this over to a discussion in that repo. Thanks! Note: some of this stuff is supported now, thought not all those cases. |
Uh oh!
There was an error while loading. Please reload this page.
I think the popularity usage of
Action<>
andFunc<>
is a proof that most of the time we don't like to declare type of generic and actually it should not neededBut
Action<>
andFunc<>
is hard to declare complex delegate. Especially function withref
andout
as parameter or return typeSo I think we should be able to always get anonymous delegate automatically from any method and lambda expression
Internally this feature would generate Action/Func or new delegate type automatically at compile time
static function is easy
Like so
instance function might be a bit tricky. the below does not work for struct
So I think maybe need to implement like this
The text was updated successfully, but these errors were encountered: