You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Whilst writing a highly general and highly generic system primarily focused on value types and dynamic communication therebetween, I have come to a conclusion that the most performant way to obtain a value of any type is with a receiver interface:
However, this results in a code looking not unlike the situation in C# 1 where there were no anonymous methods – callbacks have to be moved aside the method's code.
I'd very much like to be able to write something like this instead:
Now this requires CLR support if ValueReceiver has to inherit from System.Delegate, but there is another option: functional interfaces.
Going back to the original IValueReceiver, perhaps no additional type is needed at all: the compiler could create an anonymous type implementing IValueReceiver, closing over result. It also seems real that, upon seeing ref and the struct constraint, it would create a value type (and it should prefer so even when there is only ref).
This notion of functional interfaces could be extended to all cases, even non-generic ones. Such an interface must have no other methods than Invoke, BeginInvoke, EndInvoke, and DynamicInvoke (with the usual parameters), and thus in the end, the compiler would be able to convert any anonymous method or lambda to both delegates and interfaces that have these methods.
This is also similar to #3050 with its set of target problems and introducing new type parameters, but doesn't require reflection.
The text was updated successfully, but these errors were encountered:
Whilst writing a highly general and highly generic system primarily focused on value types and dynamic communication therebetween, I have come to a conclusion that the most performant way to obtain a value of any type is with a receiver interface:
A simple implementation of one such a receiver (to prove you can do something useful with
T
) would look like this:However, this results in a code looking not unlike the situation in C# 1 where there were no anonymous methods – callbacks have to be moved aside the method's code.
I'd very much like to be able to write something like this instead:
Analogously to normal functions, anonymous functions could be written with generic parameters and, optionally, even constraints.
This necessitates having a specific kind of delegate type with a generic
Invoke
method such as this:Now this requires CLR support if
ValueReceiver
has to inherit fromSystem.Delegate
, but there is another option: functional interfaces.Going back to the original
IValueReceiver
, perhaps no additional type is needed at all: the compiler could create an anonymous type implementingIValueReceiver
, closing overresult
. It also seems real that, upon seeingref
and thestruct
constraint, it would create a value type (and it should prefer so even when there is onlyref
).This notion of functional interfaces could be extended to all cases, even non-generic ones. Such an interface must have no other methods than
Invoke
,BeginInvoke
,EndInvoke
, andDynamicInvoke
(with the usual parameters), and thus in the end, the compiler would be able to convert any anonymous method or lambda to both delegates and interfaces that have these methods.This is also similar to #3050 with its set of target problems and introducing new type parameters, but doesn't require reflection.
The text was updated successfully, but these errors were encountered: