Given a function async Task<(Boolean result, MyObject value)> TryGetAsync()
, I can do
if((await TryGetAsync()) is var ret && ret.result)
{
//use ret.value
}
But if I try to use declare the types or use deconstruction get an error "a declaration is not allowed in this context":
//declaration. error
if((await TryGetAsync()) is (Boolean result, MyObject value) ret && ret.result)
{
//use ret.value
}
//deconstruction, also error.
if((await TryGetAsync()) is (Boolean result, MyObject value) && result)
{
//use value
}
How can I avoid using the first option var ret
in this scenario? My issue with this is that the types are not evident (which is a separate discussion).
In C# 8.0 you can use your last option and get no error:
if((await TryGetAsync()) is (Boolean result, MyObject value) && result)
{
//use value
}
The pattern matching specification doesn't allow the value tuple notation as a valid type_pattern
in the pattern matching grammar.
The tuple notation is rewritten in the compiler to use ValueTuple
. So there is an option: it does work with the underlying type, ValueTuple<bool, MyObject>
:
if ((await TryGetAsync()) is ValueTuple<bool, MyObject> ret && ret.Item1)
While not ideal, it could provide you with a workaround.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With