What are your thoughts on C# out variables? <https...
# random
a
e
Not my cup of tea. Glad Kotlin don't have it 🤷
💯 1
1
t
same. I prefer lambdas for side effects. Or use simple wrapper classes for returning multiple values
a
Agreed, I hardly see any situations where out vars are an elegant solution.
Really appreciate how Kotlin can solve many different use cases without introducing new language constructs.
d
I did a fair bit of C# and, on several occasions, would get enticed to use
out
params.. I noticed though, that I always ended up refactoring them out again, because in the long term it never proved to be the simplest/most 'API ergonomic' solution... just an anecdote but there you go.
👍 1
Destructuring might be a valid case e.g. picking out r, g, b, components from colours; but then Kotlin already elegantly provides that from data classes e.g.
val (r,g,b) = Color.fromHex("#FFBBAA")
e
I'd prefer much more
Copy code
int initializeInMethod;
OutArgExample(::initializeInMethod);
y
Maybe actually some nicer syntax for setter methods could be helpful here? because currently the
::initializeInMethod
example won't work because references to local variables aren't allowed yet. I'm thinking either
::variable.setter
could have better inlining, or maybe we could have some special syntax to refer to a setter of a property
e
of course you need to use a non-local variable
however local variable reference are coming sooner or later
j
It’s a historical product for
TryBlahblah
methods. Nowaday you should just enable nullable check and return null (unfortunately
Result<T, E>
is never easy to implemented because C# type inference is not the best).