On Saturday, 25 July 2015 at 05:27:48 UTC, Walter Bright wrote:
On 7/24/2015 11:50 AM, H. S. Teoh via Digitalmars-d wrote:
The only way to achieve this is to explicitly
negate every condition in all other overloads:

Another way is to list everything you accept in the constraint, and then separate out the various implementations in the template body using static if.

It's a lot easier making the documentation for that, too.

I've considered off and on arguing that a function like find should have a top level template that has the constraints that cover all of the overloads, and then either putting each of the individual functions with their own constraints internally or use separate static ifs within a single function (or some combination of the two). That way, you end up with a simple template constraint that the user sees rather than the huge mess that you get now - though if you still have individual functions within that outer template, then that doesn't really fix the overloading problem except insomuch as the common portion of their template constraints (which is then in the outer template's constraint) would then not have to be repeated.

However, when anyone has brought up anything like this, Andrei has argued against it, though I think that those arguments had to do primarily with the documentation, because the person suggesting the change was looking for simplified documentation, and Andrei thought that the ddoc generation should be smart enough to be able to combine things for you. So, maybe it wouldn't be that hard to convince him of what I'm suggesting, but I don't know. I haven't tried yet. It's just something that's occurred to me from time to time, and I've wondered if we should change how we go about things in a manner along those lines. It could help with the documentation and understanding the template constraint as well as help reduce the pain with the overloads. Andrei has definitely been against overloading via static if though whenever that suggestion has been made. I think that he thinks that if you do that, it's a failure of template constraints - though if you use an outer template and then overload the function internally, then you're still using template constraints rather than static if, and you get simplified template constraints anyway.

So, maybe we should look at something along those lines rather than proliferating the top-level function overloading like we're doing now.

- Jonathan M Davis

Reply via email to