Type inferencing for local function return types #7738
Replies: 2 comments
-
This was an intentional decision. It was a choice between inference or hoisting to support recursion, and the latter was chosen. Couldn't find meeting notes but here's a comment: Sounds like it could potentially be revisited but would require some limitations due to the complexity. |
Beta Was this translation helpful? Give feedback.
-
Have you tried using a lambda in-stead of a local function? Given your (slightly adjusted quick and dirty example so I could test it quickly)
LookupCustomers is inferred to be of type => Func<Expression<Func<CustomerOrder, bool>>, IOrderedEnumerable<IGrouping<object, CustomerOrder>>>? .. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I understand why
var
is only supported for locals, but it was implemented when only variables were local. Now that we have local functions, is there a reason C# should not be made to supportvar
function declarations?Motivation
Dictionary<CustomerId, Dictionary<InvoiceId, List<OrderDetails>>>
. If I want to have a local function that creates such a data structure, I have to name the type multiple times:I would like to avoid having the type in multiple places, like this:
It's cumbersome to write out the name of the type, but it's tractable. However when somebody decides that the customers need to be grouped by region and district, I fall off a cliff. I can't just change
group cust by cust.Region
togroup cust by new { cust.Region, cust.District}
because the anonymous type cannot be spoken in the function signature. Instead I'd like to be able to write it like this:Now if this happens, I have to either take the query out of its function, declare a named type somewhere else in the enclosing class, or use a
Tuple
and try to remember the region is called.Item1
and the district is called.Item2
. I'd prefer named tuples, but those aren't supported byExpressions
.Beta Was this translation helpful? Give feedback.
All reactions