Using opDispatch as *magic* getter/setter. Possible or not?
Steven Schveighoffer
schveiguy at yahoo.com
Thu Mar 31 11:38:34 PDT 2011
On Thu, 31 Mar 2011 14:02:43 -0400, Kagamin <spam at here.lot> wrote:
> Steven Schveighoffer Wrote:
>
>> The issue is that you can't have two templates with the same exact
>> template parameters, even if they have different function parameters.
>> This is because the compiler first instantiates the template, then calls
>> the function.
>
> If two template instances have the same exact template parameters, it's
> one instance, isn't it? There's no need to instantiate it twice.
Yes, but we allow function overloading. Problem is, you can't do
*template* function overloading unless you do it on the template
parameters.
For instance, these two cannot be instantiated:
void foo(string s)(int x) {writeln(x);}
void foo(string s)(string y) {writeln(y);}
because the compiler considers that you defined the same template twice
with different implementations.
However, if you do:
void foo(string s, T)(T x) if (is(T == int)) { writeln(x); }
void foo(string s, T)(T x) if (is(T == string)) { writeln(x); }
then you have two different templates, and the compiler has no issue.
This is a huge nuisance for operator overloading and opDispatch because
the only required template parameter is the string of the
operator/function name.
I suspect that fixing this will be a large change in the compiler.
-Steve
More information about the Digitalmars-d-learn
mailing list