Skip to content

Latest commit

 

History

History
55 lines (36 loc) · 1.85 KB

optional-and-named-parameters-in-expression-trees.md

File metadata and controls

55 lines (36 loc) · 1.85 KB

Support optional and named arguments in Expression trees

Champion issue: #9246

Summary

Support optional and named arguments in method calls in Expression trees

Motivation

Errors are reported for calls in Expression trees when the call is missing an argument for an optional parameter, or when arguments are named.

This results in unnecessary code and differences for expressions within Expression trees. And lack of support for optional arguments can lead to breaking changes when a new overload with an optional parameter is applicable at an existing call site.

The compiler restrictions should be removed if not needed.

For example, compiling the following with the .NET 10 preview SDK results in errors currently.

namespace System
{
    public static class MemoryExtensions
    {
        public static bool Contains<T>(this ReadOnlySpan<T> span, T value, IEqualityComparer<T>? comparer = null);
    }
}

Expression<Func<int?[], int, bool>> e;
        
e = (a, i) => a.Contains(i); // error CS0854: expression tree may not contain a call that uses optional arguments
e = (a, i) => a.Contains(i, comparer: null); // error CS0853: expression tree may not contain a named argument specification

Detailed design

Remove the error reporting for these cases in Expression trees, and allow the existing method call rewriting to handle optional and named arguments.

Drawbacks

Alternatives

Unresolved questions

It's unclear why the restrictions were added originally. An initial investigation hasn't revealed an issue supporting these cases.

Design meetings

Based on a suggestion from @roji to support optional parameters.