www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - function default arguments depending on other arguments

reply "Trass3r" <un known.com> writes:
void foo(int a, int b = a)
{
}
is illegal in C++ because order of evaluation is undefined.

But since D defines the order to be left to right couldn't it 
also allow this?
Jul 17 2014
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 07/18/2014 12:00 AM, Trass3r wrote:
 void foo(int a, int b = a)
 {
 }
 is illegal in C++ because order of evaluation is undefined.

 But since D defines the order to be left to right couldn't it also allow
 this?
It could, and I think it is an unnecessary limitation that it currently does not. (This can also be useful if that parameter is the hidden 'this' reference.)
Jul 18 2014
parent reply "Tove" <Tove fransson.se> writes:
On Friday, 18 July 2014 at 17:40:23 UTC, Timon Gehr wrote:
 On 07/18/2014 12:00 AM, Trass3r wrote:
 void foo(int a, int b = a)
 {
 }
 is illegal in C++ because order of evaluation is undefined.

 But since D defines the order to be left to right couldn't it 
 also allow
 this?
It could, and I think it is an unnecessary limitation that it currently does not. (This can also be useful if that parameter is the hidden 'this' reference.)
This request keeps popping up, I've seen it at least 3 times before and there's even an enhancement request for it: https://issues.dlang.org/show_bug.cgi?id=8075 IIRC: Walter's stance was that he needs compelling examples, which proves the utility of this new feature.
Jul 20 2014
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Tove:

 IIRC:
 Walter's stance was that he needs compelling examples, which 
 proves the utility of this new feature.
Recently I have had a desire for that feature, to write a function like this: int[][] generateTable(in uint nx, in uint ny=nx) {...} If you give just one argument to this function, it generates a square table, otherwise it uses both the given sizes. Bye, bearophile
Jul 20 2014
prev sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 7/20/14, 2:22 AM, Tove wrote:
 On Friday, 18 July 2014 at 17:40:23 UTC, Timon Gehr wrote:
 On 07/18/2014 12:00 AM, Trass3r wrote:
 void foo(int a, int b = a)
 {
 }
 is illegal in C++ because order of evaluation is undefined.

 But since D defines the order to be left to right couldn't it also allow
 this?
It could, and I think it is an unnecessary limitation that it currently does not. (This can also be useful if that parameter is the hidden 'this' reference.)
This request keeps popping up, I've seen it at least 3 times before and there's even an enhancement request for it: https://issues.dlang.org/show_bug.cgi?id=8075 IIRC: Walter's stance was that he needs compelling examples, which proves the utility of this new feature.
The classic example is injecting alloca downstream: auto fun(size_t s, void[] buffer = alloca(s)[0 .. s]) { ... } Andrei
Jul 20 2014