In C #, why lambdas don't have extensions?

In Unity, here is a category in C #,

public static class HandyExtensions { public static IEnumerator Tweeng( this System.Action<float> v, float d ) { while (..) { .. v( 13f*t ); yield return null; } v(13f); } 

Compiles perfectly!

But if you try to use it,

 yield return StartCoroutine( ( (x)=>laser=x ).Tweeng(3.141f) ); 

this typo error appears:

Assets / scripts / ... cs (116.34): error CS0023: anonymous method .' operator cannot be applied to operand of type .' operator cannot be applied to operand of type

I have tears about it.

How did C # let us go down?

Of course, is there a way to call "on" such a lambda for expansion?

By the way, the workaround should go 3.14f.Tweeng((x)=>laser=x) , but it’s not so cool.

+4
source share
2 answers

I am sorry that this upsets you, but this choice was made consciously by a team of language developers. Code that evaluates whether a given extension method is valid requires the receiver to be of a clear type, and lambda expressions not of type.

There was some debate on this, but it was ultimately decided that (1) the proposed function is potentially confusing or error prone if countless expressions, such as lambdas, method groups and null literals, become receivers of extension methods and (2) the proposed function not at all needed for LINQ to work. We were very limited in our schedules when implementing C # 3 and everything that was not necessary to reduce LINQ work. It was much easier to develop, implement, and test the "do not allow lambda as receivers" function, rather than consider all potentially odd cases where a lambda group, a method group, or zero were used as a receiver.

As others have said, you can just lay in a lambda or put it in a variable, and then use the variable as the receiver.

Alternatively, as you noticed, you might consider using float as a receiver in your specific example.

+13
source

Calm down, your tear, Joe, do not despair from your dreams! If you clearly abandoned him, he should work. Try:

 yield return StartCoroutine( ((System.Action<float>)( (x)=>laser=x )).Tweeng(3.141f) ); 
+8
source

Source: https://habr.com/ru/post/1242141/


All Articles