Can a C# lambda expression ever return void?

21,466

Solution 1

I refer you to section 7.1 of the specification, which states:

[An expression may be classified as] "nothing". This occurs when the expression is an invocation of a method with a return type of void. An expression classified as nothing is only valid in the context of a statement expression.

[Emphasis added].

That is to say that the only time you may use an expression which is a void-returning method invocation is when the expression makes up an entire statement. Like this:

M();

Solution 2

This is, in effect, a violation of functional programming rules. This has the same flaw Eric Lippert described about List.ForEach: You're philosphically trying to cause side effects on your collection.

Enumerable.Select is intended to return a new collection - filtering the input. It is not intended to execute code.

That being said, you can work around this by doing:

defaults.Where(item => item.Key.Invoke(configuration)).ToList().ForEach( item => item.Value.Invoke(configuration));

It's just not as clear as doing:

var matches = defaults.Where(item => item.Key.Invoke(configuration));
foreach(var match in matches)
    match.Value.Invoke(configuration);

Solution 3

Firstly, you should really avoid putting side-effects in standard linq query operators, and secondly this won't actually work since you aren't enumerating the Select query anywhere. If you want to use linq you could do this:

foreach(var item in defaults.Where(i => i.Key.Invoke(configuration)))
{
   item.Value.Invoke(configuration);   
}

Regarding your question, I'm pretty sure there are no possible values of void and you can't return it explicity. In functional languages such as F#, void is replaced with 'unit' i.e. a type with only one possible value - if you wanted you could create your own unit type and return that. In this case you could do something like this:

defaults.Select(item => {
    if(item.Key.Invoke(configuration))
    {
        item.Value.Invoke(configuration);
    }
    return Unit.Value;
}).ToList();

But I really can't recommend doing this.

Solution 4

From a language standpoint, void means "does not exist", which begs the question: what value would there be in declaring a variable that does not exist?

The problem here is not a language limitation but the fact that you're using a construct (the ternary operator) that demands two rvalues -- where you only have one.

If I may be blunt, I'd say you're avoiding if/else in favor of pointless brevity. Any tricks you come up with to replace default(void) will only serve to confuse other developers, or you, in the future, long after you've stopped bothering with this sort of thing.

Solution 5

default doesn't work with void; but it works with a type. The Action class produces no result, but the Func<> object always has to return a result. Whatever item.Value.Invoke() returns just return the default of that, as in:

default(object)

or if it's a specific type:

default(SomeType)

Like that.

Share:
21,466
Raghu Dodda
Author by

Raghu Dodda

Updated on May 04, 2020

Comments

  • Raghu Dodda
    Raghu Dodda over 3 years

    I have the following method, and I want to know if there is anything that can go in place default(void) below because there is a compiler error that says that void is not valid here:

    private void applyDefaultsIfNecessary(ApplicationConfiguration configuration)
    {
        var defaults = new Dictionary<Predicate<ApplicationConfiguration>, Action<ApplicationConfiguration>>()
        {
           // { rule, action } - if rule is true, execute action 
           { (c) => c.ConnectionString == null , (c) => c.ConnectionString = "foo" },
           { (c) => c.OutputExcelFilePath == null, (c) => c.ConnectionString = "bar" },
           { (c) => c.OutputDirectory == null, (c) => c.OutputDirectory = "baz" }
    
        };
    
        //Nothing to select, but we want to loop throough the dict and invoke action, if rule is true.
        //It is a pity there is no extension method called DoForEach on collections.
        defaults.Select((item) => item.Key.Invoke(configuration) ? item.Value.Invoke(configuration) : default(void)  );
    }
    

    I realize that I can use the if-else statement instead of the ternary operator (or that I could call a dummy method to return void). Also, the Select extension method does not like lambdas that return void. It seems to say that the type cannot be inferred, but of course if I specify the type like this, either:

    defaults.Select<ApplicationConfiguration, void>((item) => { if (item.Key.Invoke(configuration)) item.Value.Invoke(configuration); } );
    

    I was curious from a language design standpoint, why we don't have expressions that can return void or the data type for variables that is void.

  • Raghu Dodda
    Raghu Dodda almost 14 years
    Thanks. I understand that, but my question is if there is something else that can go in place of default(void).
  • Raghu Dodda
    Raghu Dodda almost 14 years
    I agree with you about pointless brevity. I would not do this in production code; I am just trying to grok lambdas in some toy code I am writing.
  • Raghu Dodda
    Raghu Dodda almost 14 years
    Very good point about not enumerating it and info about the unit type. Thanks.
  • Brian Mains
    Brian Mains almost 14 years
    yes default(<type>) that is what I'm saying. You have to return a default of a type, not void. You can't do void with a FUnc<>.
  • Raghu Dodda
    Raghu Dodda almost 14 years
    Perfect. +1 for answering the exact question.
  • Jeppe Stig Nielsen
    Jeppe Stig Nielsen over 9 years
    Also, a void expression can be the right-hand side of the => lambda arrow. The entire expression including the arrow does not have a type in itself. And in the case where the => needs to be converted to an expression tree, that cannot be writtens as a block with a single statement in it. For example Expression<Action> tree = () => Console.WriteLine("Hello"); is legal and appears to contain a sub-expression of type void. Expression<Action> tree2 = () => { Console.WriteLine("Hello"); }; will not compile because the statement body { ... } is not allowed for expression trees.
  • Eric Lippert
    Eric Lippert over 9 years
    @JeppeStigNielsen: Good point. Of course the right side of a lambda operator is a context in which a statement expression is legal. And it has always struck me as odd that it is legal to have "nothing" expressions in an expression tree. Expression trees were designed to represent side-effect-free operations, but a void-returning method is almost certainly there for a side effect.
  • Tim
    Tim over 6 years
    (1) Is "a void-returning method invocation" both an expression and a statement? (2) stackoverflow.com/q/44234246/156458
  • Eric Lippert
    Eric Lippert over 6 years
    @Tim: no. An expression statement ends in a semicolon. An expression does not.
  • Tim
    Tim over 6 years
    @Eric: Thanks. At stackoverflow.com/q/44234246/156458, i am wondering: (1) Is a call to a method always an expression, whose value is the value returned by the method? (2) is a void expression always an invocation of a method which returns void?
  • Brian
    Brian over 6 years
    As of C# 7, you can throw in an expression.