3 ms·
I really like the way C# did this: void foo<T>(T by_value, ref T by_ref, in T by_immutable_ref, out T out_only); Append `?` to any type to make it optiona
by mastax 5y ago
I really like the way C# did this:
void foo<T>(T by_value, ref T by_ref, in T by_immutable_ref, out T out_only);
Append `?` to any type to make it optional. (Though that is more of a lint than truly part of the type system).
`out` parameters pass no value in and must pass a value out. Isn't that just a return value, semantically? Yes, but we didn't used to have value tuples and destructuring so we had to make do. It can still be nicer for some things:
bool TryParse(string input, out int value)
if (int.TryParse("123", out int value))
// Use value
- Jenk 5y agoThen async comes along and you can't use `out` in async methods :) Thus (anonymous) tuples were introduce so we can do it Go/Rust-style: async Task<(bool, T)> Foo<T>(T value) { /* do something async like a web request or file read etc. */ var aThing = await GetAThing<T>(value); return (aThing != null, aThing); }