On Thursday, 26 October 2017 at 12:19:33 UTC, Steven Schveighoffer wrote:
D's lookup rules fail miserably when it comes to templates:

mod1.d:

auto callFoo(T)(T t)
{
  return t.foo;
}

mod2.d:

struct S
{
   int x;
}

int foo(S s) { return s.x * 5; }

void main()
{
   auto s = S(1);
   assert(s.foo == 5);
   assert(s.callFoo == 5); // can't compile
}

Would be nice to have a way around this. Not sure what it would look like.

Assuming you don't want to change the original struct, this can be worked around by making a wrapper type using alias this. I think that's logical because you have to be explicit about which foreign functions you want the imported algorithm to see. There would be a function hijacking problem otherwise.

What is the catch here, is that alias this won't solve cases where the free function takes a reference to the wrapped type or returns it.

Reply via email to