Minor problems with templates and mixins
Oskar Linde
oskar.lindeREM at OVEgmail.com
Tue May 23 06:39:59 PDT 2006
Tom S skrev:
> While D's template system is really excellent, it's still got minor
> problems. One is related to the current way to define function
> templates. The other is about mixins - they seem to have a really
> awkward nature.
>
> * The two issues I present are by no means showstoppers, but rather
> language details that make it unpleasant to use in a few cases.
>
>
> 1. Can't overload functions inside templates, e.g. the following code
> won't work
>
> # template foo(T) {
> # T foo(int a) { return T.init; }
> # T foo(char a) { return T.init; }
> # }
> #
> # void main() {
> # foo!(float)(1);
> # }
>
> Yeah, the spec mentions that to be able to write foo!(float) instead of
> foo!(float).foo, the template must have exactly one symbol in it, yet
> it's not very comfortable to work with.
I posted about this a while ago too. For this case, the following
workaround seems to work:
template foo_(T) {
T foo(int a) { return T.init; }
T foo(char a) { return T.init; }
}
template foo(T) {
alias foo_!(T).foo foo;
}
void main() {
foo!(float)('a');
}
> It's possible to partially work around this limitation, e.g.
[snip]
> But this idea breaks when the function template is meant to be a
> non-static member of some class.
The template-typedef(alias) workaround seems to work as a non-static
member too.
Regards,
Oskar
More information about the Digitalmars-d
mailing list