opCall and template functions

Jarrett Billingsley jarrett.billingsley at gmail.com
Thu May 21 07:13:49 PDT 2009


On Thu, May 21, 2009 at 10:01 AM, Steve Teale
<steve.teale at britseyeview.com> wrote:
> Is this error intentional
>
> import std.stdio;
>
> class A
> {
>   int a;
>   double b = 22.4;
>   string s = "whatever";
>
>   string opCall() { return s; }
>   //T opCall(T = string)() { return cast(T) s; }
>
>   T opCall(T)(T* dummy)
>   {
>      if (is(typeof(dummy) : int*))
>         return cast(T) a;
>      else
>         return cast(T) b;
>   }
> }
>
> double* D;
> int* I;
>
> void main()
> {
>   A a = new A();
>   double d = a(D);
>   writefln("d = %f", d);
>
>   int n = a(I);
>   writefln("n = %d", n);
>
>   writefln(a());
> }
>
> strange.d(10): Error: template strange.A.opCall(T) conflicts with function strange.A.opCall at strange.d(8)
>
> You can make it work with the template version of opCall with no args

I doubt it's intentional, but that's always been the workaround.  One
of the things Walter said he wanted to do for D2 was to make it
possible to overload templated and normal functions like this, but
that sadly hasn't materialized yet.



More information about the Digitalmars-d mailing list