ref and out parameters on call site
WebFreak001
d.forum at webfreak.org
Wed Mar 18 17:23:26 UTC 2020
I would like to bring it up yet again! (I think this is my 3rd
time talking about it lol)
Can we get make the call site of a ref/out function repeat the
ref/out keyword like in C#?
Example:
void foo(double a, ref string s, out int[] x);
called with:
foo(doubleValue, ref stringValue, out intArray);
My basic idea was this:
* Force usage of ref/out on call site, otherwise cause
deprecation for now.
* If function is called with UFCS, ref is not needed on first
argument (before the .)
Reason:
It's currently impossible to know if a function argument mutates
a variable or not without jumping into the definition or
documentation. Code reviews get orders of magnitude more
difficult to get right when calling new functions you aren't
familiar with.
I think this would massively help readability and simplify static
analysis and code reviews. This also has the added benefit that
you can easily unambiguously call both ref and non-ref overloads
using an lvalue. It also makes it possible to _require_ functions
like template callbacks to have a ref/out parameter.
If we were to go this way, we could go all the way and even allow
const/immutable construction out of "out" arguments:
if (generateSomething(out const something)) {
writeln("yay, something: ", something);
}
Any ideas? Counter arguments why this shouldn't be implemented
other than old code breakage?
More information about the Digitalmars-d
mailing list