"return [xxx] if (condition);" one-liner syntax proposal. #8320
Replies: 6 comments 6 replies
-
See: #2195 It's already a one-liner: if (result.IsError) return result.Error; |
Beta Was this translation helpful? Give feedback.
-
Since you mentioned monads, I would love to have private static async Task<Result<Response>> AddProductToStore(
[FromBody] Request request,
[FromServices] IRepository<Store> storesRepository,
[FromServices] IRepository<Product> productsRepository,
[FromKeyedServices(nameof(Inventory))] IUnitOfWork unitOfWork,
CancellationToken cancellationToken)
{
var store = yield await storesRepository.SingleOrDefaultAsResultAsync(new StoreByIdSpec(request.StoreId), cancellationToken);
var product = yield await productsRepository.SingleOrDefaultAsResultAsync(new ProductByIdSpec(request.ProductId), cancellationToken);
var storeProduct = store.AddProduct(product.Id, request.Quantity, request.Price);
await unitOfWork.SaveChangesAsync(cancellationToken);
return new Response(storeProduct.Id);
} void Result<int> M(Result<int> first, Result<int> second)
{
return (yield first) + (yield second);
} Would desugar to: void Result<int> M(Result<int> first, Result<int> second)
{
if (first.IsFailure) return first.Error;
var firstValue = first.Value;
if (second.IsFailure) return second.Error;
var secondValue = second.Value;
return firstValue + secondValue;
} Attributes or a witness struct would have to be used in order to let the compiler know how to generate the pattern given your custom |
Beta Was this translation helpful? Give feedback.
-
So many Pythonic proposals, and they're all rejected: C# is a C-like language, not a Python-like one. |
Beta Was this translation helpful? Give feedback.
-
Quoting myself: #7962 (comment)
|
Beta Was this translation helpful? Give feedback.
-
[jnm2: removed quote of entire post]Remove the curly braces and put the return statement right next to the the if condition and you got yourself a one-liner syntax |
Beta Was this translation helpful? Give feedback.
-
I would argue that the problem lies elsewhere - if you used exceptions in the first place, you wouldn't have to validate the return codes... so many years with exceptions and people are still using bool/int/enum return codes... and the argument "this is the API we have to work with" could be countered by "then make your own anti-corruption layer that will encapsulate the external API"... |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Result pattern/monad is becoming popular day by day. I also have a custom implementation on my projects. We usually can chain methods if we add extension methods to our Result class. But sometimes, we can't always chain methods or we don't want to. For this situations, we have to manually write this kind of flow-control code (it may differ depending on your implementation):
For all this kind of situations (it may not be relevant to result pattern at-all of course), I have a new syntax proposal. It is
OR
Here is an example without this one-liner syntax (as we curently don't have):
With new syntax it could become:
This could increase readability and make language less verbose in such situations.
Beta Was this translation helpful? Give feedback.
All reactions