https://issues.dlang.org/show_bug.cgi?id=19057

--- Comment #4 from johanenge...@weka.io ---
My concern is not about use cases. It's about breaking old code (silently), and
also about strange (arguable) language behavior in the current situation.

Note that before 2.079, the default argument was never used: the template would
not match (in the testcase, `bar("a","b");` would not compile pre-2.079).
(which is probably why Timothee wrote that it now becomes possible to use
default args)

Breaking old code:
The fact that default argument was never used in pre-2079 is bad news: it means
that old code always specifies a value for the default value, and thus that
with the new compiler things are always broken (except when the caller is not
using IFTI, which is unlikely I think).

Strange new behavior:
Adding a default value for a parameter now changes the template instantiation
(if IFTI is used, which I think will happen in the majority of cases).
```
// Adding the default value will change behavior.
void bar(Args...)(Args _args, int timeout /* = 0 */) {
    writeln(_args, timeout);
}
void main() {
    bar("a", "b", 2); // used to print "ab2", but since 2.079 prints "ab20"
}
```
How many people would expect that to happen?


The use case of the new style does not discuss much about how to override the
default value (something that is trivially explained for normal functions).
Here, you need to disable IFTI, which is painful. I've mainly seen discussion
of the use case with __FILE__, but not for the general case.
Note: there is no old use case, because the default value itself didn't work
(syntax was allowed, but always need to specify value for the parameter at call
site).
A better fix for the __FILE__ use case would perhaps have been something like
this:
`void bar(Args...)(Args args, TypeThatIsNeverMatched =
TypeThatIsNeverMatched.init, string file = __FILE__)`, where default arguments
work but IFTI matching on callsite first matches the non-variadic parameters,
and then starts matching the variadic part.

--

Reply via email to