Metadata Attributes on Lambda Expressions #3115
Replies: 5 comments
-
What are the use cases where attributes on the method that is called when you invoke a delegate matter? |
Beta Was this translation helpful? Give feedback.
-
@svick, there are plenty of use cases. A few are indicated:
void Main()
{
new Test().GetNumber(new[] { 4, 1, 3, 2, 5 }, ienum => ienum.OrderBy(i => i).Where([StacktraceName("GetNumberLambdaWhereCondition")] (i) => i != 2 ? throw new Exception("Stacktrace") : true).First());
}
class Test
{
public int GetNumber(IEnumerable<int> ienumerable, Func<IEnumerable<int>, int> predicate)
{
return predicate(ienumerable);
}
} void Main()
{
new Test().GetNumber(new[] { 4, 1, 3, 2, 5 }, ienum => ienum.OrderBy(i => i).Where([DisplayName("GetNumberLambdaWhereCondition")] (i) => i != 2 ? throw new Exception("Stacktrace") : true).First());
}
class Test
{
public int GetNumber(IEnumerable<int> ienumerable, Func<IEnumerable<int>, int> predicate)
{
return predicate(ienumerable);
}
}
[CompilerGenerated]
[Rule]
[HasForm("f3(X, Y) := f1(X), f2(Y).")]
public static IEnumerable<bool> f3(Term X, Term Y)
{
// ...
} [Rule] [HasForm("f3(X, Y) := f1(X), f2(Y).")] [DisplayName("f3")] (Term X, Term Y) => ...
[MethodImpl(MethodImplOptions.AggressiveInlining)] (int x, int y) => x + y;
|
Beta Was this translation helpful? Give feedback.
-
At thsi point, it's somewhat defeating the point of lambdas (small, lightweigh pieces of code). I'd just as soon use a local-function if it's going to need so much ceremony. |
Beta Was this translation helpful? Give feedback.
-
C# lambda expressions may have originally been designed and intended for lightweight scenarios, e.g. LINQ. Extending the A scenario which is both useful and interesting to me is that of runtime code generation, constructing In addition to On the topic of local functions vs. lambda expressions, I found this: https://docs.microsoft.com/en-us/dotnet/csharp/local-functions-vs-lambdas . [1] https://josephwoodward.co.uk/2016/12/in-memory-c-sharp-compilation-using-roslyn |
Beta Was this translation helpful? Give feedback.
-
Most of those use cases sound purely hypothetical to me, I don't think it makes sense to add a feature to the language that could be useful, there has to be a near certainty that it will be useful to a sufficiently large number of developers. Specifically:
|
Beta Was this translation helpful? Give feedback.
-
I would like to propose for consideration some syntax for utilizing metadata attributes on lambda expressions such that compiled-to methods would be decorated or adorned with the custom attributes.
For an example scenario:
one could adorn a lambda expression with attributes:
one could decorate or adorn the parameters:
and one could decorate or adorn the return value:
These can work in combination:
The attribute usage for the attributes upon lambda expressions would be that for methods, as the resultant attributes decorate or adorn the compiled methods.
With C# language syntax resembling that indicated above, one could compile lambda expressions and resultant methods would be decorated or adorned with specified metadata attributes. This would be useful when developers desire to make use of metadata attributes and to utilize compiled and runtime-compiled methods interchangeably.
Beta Was this translation helpful? Give feedback.
All reactions