Open Liero opened 1 month ago
Sounds like that could be an external source generator, perhaps combined with an interceptor (to intercept the Compile()
call)
A compiler should be able to optimize a compile time declared LambdaExpression so that the Compile() is fast.
The word should here though is rather vague. It's possible yes that it should be able to but what is the design that leads to us doing this?
Background and Motivation
Many UI Frameworks and libraries like Blazor, data grids, mappers, etc. required both
LambdaExpression
in order get reflection metadata such as PropertyInfoFunc
ekvivalent of the LambdaExpression (compiled lambda) in order to get actual valueFor example:
or
The library authors either go for runtime compilation (slow) or requires consumers to declare both, LambdaExpression and Func, although they look the same (DRY violation).
Proposed Change
A compiler should be able to optimize a compile time declared
LambdaExpression
so that the Compile() is fast. If lambda expression parameter types, return type and captured variables' types are known to the compiler, it should be possible to generate Func<>, shouldn't it?There is actually no public API change needed.
A naive example
Roslyn will generate
Func<string>
equivalent and associate it with the lambdaIt's probably more difficult when there are captured variables, but some precompilation steps should be possible so that
LambdaExpression.Compile()
is fast.Alternative Designs
A razor compiler actually generates both, LambdaExpression and Func for
@bind
directive. Consider more general approach, such asSystem.Runtime.CompilerServices
attributes similar to[CallerMemberName]