inline or sync keyword for function callback #2500
Replies: 9 comments
-
You use the example of |
Beta Was this translation helpful? Give feedback.
-
@HaloFour That's why I propose Like generic constraint by interface that you don't cast object passed in into that interface. It just define contract that the object passed in must be that interface, then it actually generate function for that object In other word. This maybe just a syntax that would transpile syntax tree to inline. And that would make you able to call |
Beta Was this translation helpful? Give feedback.
-
That's not possible, the BCL method requires a delegate. There's nothing else that the C# compiler could pass to it that would satisfy that requirement.
It couldn't transpile the call to
That's not what the C# compiler does at all. The compiler just dispatches the calls to the required members of that interface to the generic type, which is guaranteed to succeed by the CLR enforcing the generic type constraint. There are no other generated functions. |
Beta Was this translation helpful? Give feedback.
-
I think this could work only if the C# compiler know exactly what But neither is true: the C# compiler often compiles against reference assemblies, which don't contain any code. And even if you compile against assemblies with code, the user is free to use different implementation when running the code. |
Beta Was this translation helpful? Give feedback.
-
This should be a JIT feature. There could be a heuristic that makes the JIT inline away the entire iterator machinery, stack allocate all temporary objects and derive the exact types and delegate targets for all relevant objects in order to devirtualize everything. There's no reason this could not be as fast as an optimal hand-written loop. |
Beta Was this translation helpful? Give feedback.
-
@GSPP I agree that's the best way to get something similar. But it won't help with the motivating example, which was about accessing a |
Beta Was this translation helpful? Give feedback.
-
@HaloFour I think you could try by create a generic class like this class TT<T>
{
static T[] Array = new T[10];
}
static void Main()
{
if(!TT<int>.Array.Equals(TT<long>.Array))
throw new Exception("Generic generate new class");
} Generic is not just dispatch the call. If it is value type it would pass that object without boxing to interface. I am quite sure that it generate new things map to each type I would have |
Beta Was this translation helpful? Give feedback.
-
I don't understand why inline your lambda into We all know generic types will be generated for every type parameter usage, but it's not the compiler's job, at least when targeting Desktop CLR. The actual types are generated at runtime, by the JIT compiler. The JIT compiler do can inline |
Beta Was this translation helpful? Give feedback.
-
@CnSimonChan The problem that we can't use So if we could have a feature to enforce immediately call then it possible to access reference arguments And if modified current implementation can cause problem. Well, we could just create new library to support it. And to compile program to work as we want is compiler's jobs. It is programmer jobs to do optimization first hand before JIT. And I request that programmer should have access to the tools that can order JIT to do work as we wish it to be like |
Beta Was this translation helpful? Give feedback.
-
async callback is powerful feature. But sometimes it overkill to enforce all callback pass in function
First limitation is callback function cannot using ref parameter unless we copy it
And Sometimes function is just iterator or little extension method that could and should be inlined both callback and the function itself
So I think we should have
inline
orsync
keyword at least at function parameter. And the function written at callback could access ref parameterBeta Was this translation helpful? Give feedback.
All reactions