Inferred Method Return Types #4086
-
I think that allowing methods to infer their return types could hugely cleanup functional code. For example: public async Task<IEnumerable<(string FirstName, string? MiddleName, string LastName)>> GetCustomerNames(IEnumerable<int> customerIds)
=> (await _client
.Post<Customer[]>($"customers/bulk", customerIds))
.Select(customer => (customer.FirstName, customer.MiddleName, customer.LastName)); Could instead be written as: public async GetCustomerNames(IEnumerable<int> customerIds)
=> (await _client
.Post<Customer[]>($"customers/bulk", customerIds))
.Select(customer => (customer.FirstName, customer.MiddleName, customer.LastName)); I think this becomes even more valuable when working with discriminated union types. I find the generics can become unwieldly pretty fast. In that scenario, this: public Task<Result<Order, OrderNotFound>> GetOrder(Guid orderId)
=> _client
.Get<Order>($"orders/{orderId}")
.MapFailure(_ => OrderNotFound.Value);
public Task<Result<Customer, CustomerNotFound>> GetCustomer(int customerId)
=> _client
.Get<Customer>($"orders/{orderId}")
.MapFailure(_ => Customer.Value);
public Task<Result<Option<string>, Union<OrderNotFound, CustomerNotFound>>> GetCustomerNameForOrder(Guid orderId)
=>
from _ in Result.Unit<Union<OrderNotFound, CustomerNotFound>>()
from order in GetOrder(orderId)
from customer in GetCustomer(order.CustomerId)
select Option.Create(customer.HasName, () => $"{customer.FirstName} {customer.Lastname}"); Could become this: public GetOrder(Guid orderId)
=> _client
.Get<Order>($"orders/{orderId}")
.MapFailure(_ => OrderNotFound.Value);
public GetCustomer(int customerId)
=> _client
.Get<Customer>($"orders/{orderId}")
.MapFailure(_ => Customer.Value);
public GetCustomerNameForOrder(Guid orderId)
=>
from _ in Result.Unit<Union<OrderNotFound, CustomerNotFound>>()
from order in GetOrder(orderId)
from customer in GetCustomer(order.CustomerId)
select Option.Create(customer.HasName, () => $"{customer.FirstName} {customer.Lastname}"); I've also found that writting fluent libraries can sometimes produce some very large generics that could be nicely simplified (although in that scenario you need to be careful not to alter your contracts by accident). I think this would bring one of the major benefits of F# to C#. Thoughts? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This has been discussed a number of times before, for example #1484. Whilst the desire for this is understandable, the team has expressed that due to the inherent difficulties involved they have no plans to do this. |
Beta Was this translation helpful? Give feedback.
This has been discussed a number of times before, for example #1484. Whilst the desire for this is understandable, the team has expressed that due to the inherent difficulties involved they have no plans to do this.