| Thread overview | |||||
|---|---|---|---|---|---|
|
May 21, 2009 opCall and template functions | ||||
|---|---|---|---|---|
| ||||
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
| ||||
May 21, 2009 Re: opCall and template functions | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Steve Teale | On Thu, May 21, 2009 at 10:01 AM, Steve Teale <steve.teale@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.
| |||
May 22, 2009 Re: opCall and template functions | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley Wrote:
> On Thu, May 21, 2009 at 10:01 AM, Steve Teale <steve.teale@britseyeview.com> wrote:
> > Is this error intentional
> > 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.
Jarrett,
I can see just intuitively that it could be one of those difficult things to do, so I'll continue with the workaround, and live in hope. But I do appreciate the intention.
Steve
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply