www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Vararg-templated class with matching member func

reply Nick Sabalausky <SeeWebsiteToContactMe semitwist.com> writes:
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.
Apr 06 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Apr 06 2013
parent Nick Sabalausky <SeeWebsiteToContactMe semitwist.com> writes:
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
Apr 06 2013