It would be great if we had the opportunity of out parameters.
For example:
void main() {
int i = 1;
void inc(int i) { i++; };
inc(i);
print(i);
}
result: 1;
Proposal:
void main() {
int i = 1;
void inc(**ref** int i) { i++; };
inc(i);
print(i);
}
result: 2
In many languages it supported. C++, Pascal, php.
I known workaround, but this future will be a great extend of our platform!
Sorry, but I have to disagree with you. This would bring no benefits and would simply open space for unsafer programs.
I don't see this as an improvement...
Sorry, but I have to disagree with you. This would bring no benefits and would simply open space for unsafer programs.
I don't see this as an improvement...
Coud you please explain unsafe code? My base language - pascal. Out params is a normal case, in php and c++ too
@NickNevzorov
In a fundamental perspective, a function should be pure whenever possible. This is not only clearer but also safer.
For example, instead of:
int i = 1;
inc(i);
It's clearer to have:
int i = 1;
i = inc(i);
Of course, the example you have given is a trivial one, but most real-world cases are not. In the second case, you have a pure function, where you know that you have a reproducible and testable program.
The problem goes even worse if the out parameter changes the behavior os the inner function. This way the parameter acts like an in-out parameter, and the behavior becomes state-dependent.
Have a look at this source:
https://stlab.cc/tips/stop-using-out-arguments.html
I'm not saying that reference parameters are useless. They actually are usually more performatic than value parameters. This is why performance/memory critical environments, like embedded systems, usually use C/C++. They are lower-level languages that MUST deal with pointers and direct memory access. This is not the case of Dart, tho, and thus I see no reason to implement reference values.
Ps. As a PHP programmer myself I never used by-ref parameters and I've never seen any need of this, so I can't agree with you that they are a normal case in PHP. In C++, tho, I agree they are, but the context is completely different.
At the moment, I think we're leaning more in the direction of supporting multiple return values with destructuring, in part because it generalizes more broadly. cc @munificent
I currently return multiple values with Tuples (available on dartz or tuples package), but it would be ways better to have in-language tuples with destructuring, as well as sum types.
At the moment, I think we're leaning more in the direction of supporting multiple return values with destructuring, in part because it generalizes more broadly. cc @munificent
I agree with You. Multiply return values will be a great improvment then out parameters!
Most helpful comment
At the moment, I think we're leaning more in the direction of supporting multiple return values with destructuring, in part because it generalizes more broadly. cc @munificent