On Tuesday, 27 August 2013 at 07:54:06 UTC, Walter Bright wrote:
On 8/27/2013 12:45 AM, monarch_dodra wrote:
The root reason for this mechanism, is that it allows writing
templated functions that can operate on objects from any
namespace.
I believe the root reason was so that operator overloads would
work when the rvalue was the class type. (In C++ you have to
write non-member functions to handle those cases.)
D solves that via other mechanisms, so Koenig lookup is not
necessary.
The only reason slices work as ranges in D is because std.range
imports std.array. As in the example in the original post, for
slices, front, popFront, and empty are free functions, so
std.range cannot find them without importing std.array directly.
As a result, slices are the ONLY ranges that can do this, which
is a bit hacky.
I'm not sure what the resolution of this is. Anti-hijacking is
import, and ADL can get quite messy, but ADL is also important.
We may just have to ensure that all template interfaces are
satisfied via member functions.