Thread overview | |||||
---|---|---|---|---|---|
|
April 06, 2013 Vararg-templated class with matching member func | ||||
---|---|---|---|---|
| ||||
I have a feeling I'm missing something obvious, but how do I create something like this?: class Foo(TArgs...) { void func(/+ args must exactly match TArgs +/) { } } new Foo(int).func(5); // Ok new Foo(int, string).func(5, "hello"); // Ok // Error! Overload not found! new Foo(int).func(5, 8); new Foo(int).func("hellO"); // Error! Overload not found! new Foo(int, string).func(5); new Foo(int, string).func("hello"); Maybe it's just a slow-brain day, but I'm having trouble working out the right syntax for that. |
April 06, 2013 Re: Vararg-templated class with matching member func | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | Nick Sabalausky:
> I have a feeling I'm missing something obvious, but how do I create
> something like this?:
>
> class Foo(TArgs...)
> {
> void func(/+ args must exactly match TArgs +/)
> {
> }
> }
>
> new Foo(int).func(5); // Ok
> new Foo(int, string).func(5, "hello"); // Ok
>
> // Error! Overload not found!
> new Foo(int).func(5, 8);
> new Foo(int).func("hellO");
>
> // Error! Overload not found!
> new Foo(int, string).func(5);
> new Foo(int, string).func("hello");
>
> Maybe it's just a slow-brain day, but I'm having trouble working out
> the right syntax for that.
Do you mean something like this?
class Foo(TArgs...) {
void func(TArgs args) {}
}
void main() {
(new Foo!(int)).func(5); // Ok
(new Foo!(int, string)).func(5, "hello"); // Ok
// Error! Overload not found!
(new Foo!(int)).func(5, 8);
(new Foo!(int)).func("hellO");
// Error! Overload not found!
(new Foo!(int, string)).func(5);
(new Foo!(int, string)).func("hello");
}
The extra () around the new is something that we are asking to be not required. But Walter for unknown reasons seems to not like the idea...
Bye,
bearophile
|
April 07, 2013 Re: Vararg-templated class with matching member func | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Sun, 07 Apr 2013 01:53:34 +0200 "bearophile" <bearophileHUGS@lycos.com> wrote: > Nick Sabalausky: > > > I have a feeling I'm missing something obvious, but how do I > > create > > something like this?: > > > > class Foo(TArgs...) > > { > > void func(/+ args must exactly match TArgs +/) > > { > > } > > } > > > > new Foo(int).func(5); // Ok > > new Foo(int, string).func(5, "hello"); // Ok > > > > // Error! Overload not found! > > new Foo(int).func(5, 8); > > new Foo(int).func("hellO"); > > > > // Error! Overload not found! > > new Foo(int, string).func(5); > > new Foo(int, string).func("hello"); > > > > Maybe it's just a slow-brain day, but I'm having trouble > > working out > > the right syntax for that. > > Do you mean something like this? > > > > class Foo(TArgs...) { > void func(TArgs args) {} > } > > void main() { > (new Foo!(int)).func(5); // Ok > (new Foo!(int, string)).func(5, "hello"); // Ok > > // Error! Overload not found! > (new Foo!(int)).func(5, 8); > (new Foo!(int)).func("hellO"); > > // Error! Overload not found! > (new Foo!(int, string)).func(5); > (new Foo!(int, string)).func("hello"); > } > > > The extra () around the new is something that we are asking to be not required. But Walter for unknown reasons seems to not like the idea... > Actually, the missing parens around new were just part of my example, not in my original code. The "void func(TArgs args) {}" is something I had tried, but apparently my real problem was something else: class Foo(TArgs...) { void func(TArgs args) { func2(args); } void func2()(TArgs args) {} } void main() { (new Foo!(int)).func(5); } That fails. If func2 is *not* a template then it works. Looks like I stumbled on a compiler bug. And idea offhand whether it's a known one? I'm going to go ahead and file it, feel free to close if you know it's a duplicate (I didn't see anything like it though): http://d.puremagic.com/issues/show_bug.cgi?id=9894 |
Copyright © 1999-2021 by the D Language Foundation