Why can’t an anonymous method be assigned to a var or Dynamic?

Asked

Viewed 390 times

5

The following code:

var mostra = delegate(string x)
{
    Console.WriteLine(x);
};
mostra("teste");

The . Net should not identify the var as a Action<string>?

And the same for dynamic?

2 answers

5

Miguel Angelo’s answer already answers the question. I’ll just post some information that might help find some other solution in some cases. Of course, for the question, the simplest and most obvious solution is the second alternative to his answer.

The var is not a magic solution and should always be used, is just a facilitator and when it is not possible to use it just do as it was before that is to use the type explicitly. I don’t like the solutions of cast, even though they work. It would only be useful in the case of dynamic since this command "hides" the compile-time type.

Note that this is such a difficult problem to solve that even in Runtime it is not possible to identify unambiguously which type is correct, hence the dynamic also does not solve.

Why is it difficult to infer in this case?

Eric Lippert, the creator of this feature in the compiler, already explained in his response in the OS because infer the type of delegate would be too complicated or even impossible.

Abuse of variable use

One thing to think about is whether you need a variable in this case. One thing I realize to be very common is programmers finding that variable is something absolutely necessary in a program. And it’s not. Variable is just a project pattern (Pattern design).

Of course the code would be unreadable but it is possible to program without the use of variables. And amaze, respecting the DRY.

I’m not preaching this but as I say exceptions are being abused I say that programmers abuse variables all the time. Probably because they don’t understand that variable is just a place to store data temporarily. Do not understand that if there is no advantage in keeping this data, should not keep it.

Interestingly because they don’t understand this, they make the wrong mistake too. When variables can be used, they end up not using it. I’m talking about this in that and in that reply. People don’t understand that expressions can be replaced by variables, so where she knows she needs an expression, she is forced to create an expression when a variable is enough. I never get tired of seeing this freak:

if (booleanoQualquer == true)

Of course, the variable can be used as a form of documentation of the steps that are being made but it is not its main function, it is a side effect.

Completion

So it’s nice to know that the real problem is with var and not with the inference itself. It is possible to infer when there is enough information to infer. If it were not in the var yes in a place where one expects an expression of a specific type, the compiler can find out if they are compatible. This is well shown in this blog. See the code of it running.

If you don’t have to actually use a variable, don’t use it and gain the inference in most cases.

I’m not saying you abused because I know it was just a self-contained example.

  • I just disagree with the point you make "variable is just a place to store a value", because the variable has a name, its use can also be to document, and in addition it can also be used as an organization unit. Example: a complex calculation divided into several steps (even if each step is used only once)... the rest I agree with everything, mainly with regard to the abuse of variables, which occurs mainly with the less experienced developers. I’ve seen some codes where each row is a new variable. So it’s +1.

  • So you don’t disagree because I say that without it the code would be unreadable. Of course I use a lot of variables, even just to document. I talk in more detail links that I showed. And in other answers I speak as code readable and better than code commented: http://answall.com/a/15566/101. What is important is that people need to understand the function of each thing in a code, they program but do not understand the structure of a code, then they can’t even use their creativity. You have to know why you put sugar in tomato sauce, otherwise you can’t make yummy noodles.

  • Exactly! I just disagreed with that specific phrase... more specifically the word only. Your common sense is remarkable, so I won’t disagree with many things either... =).

  • 1

    I like the analogy you made with spices. Just as the use of spices should be balanced in cooking, the use of language resources should be balanced in programming. Both lack and exaggeration produce not so appreciable results. That’s where the "common sense" of "Heff" comes in... he must know the customer, their culture, their desires. The developer too, knowing that his customers are his company, his users, and also his developer friends, and even himself at a future time.

  • The "only" was more in the sense of what is done with the variable. Documentation is a side effect and the only way to document without storing the value is with comment. But you may disagree, I know bullshit and I want to know when the case is :) The comment takes another bias of what I said. You have to know what each seasoning, causes and consequences serve to use it together with others and know what will produce something desirable.

4


Enough for you to ask yourself what kind of var then?

In the case of your example:

var mostra = delegate(string x)
{
    Console.WriteLine(x);
};
mostra("teste");

mostra would be a delegate of what kind? Action<string>? Or would it be some other guy... for example, I could invent some kind of a delagate:

delegate void Xpto(string str);

And now?

There is no way to know, because delegates are objects that have type in . Net.

How then

There are a few ways for you to indicate delegate type to C#:

var mostra = (Action<string>)delegate(string x)
{
    Console.WriteLine(x);
};
mostra("teste");

Or else:

Action<string> mostra = delegate(string x)
{
    Console.WriteLine(x);
};
mostra("teste");

Or else:

var mostra = (Action<string>)(x => Console.WriteLine(x));
mostra("teste");

The latter being an alternative from my first example, only using a lambda to generate delegate.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.