digitalmars.D - Can't pass delegate to template
- Kramer (30/30) Feb 09 2005 I'm trying to pass a delegate to a template but keep getting the error s...
- Niall FitzGibbon (9/36) Feb 10 2005 It seems that the template itself is expecting a type parameter but
- Stewart Gordon (9/15) Feb 10 2005 Correct.
- Kramer (3/18) Feb 10 2005 Compiles, but doesn't print. Maybe it's half-way supported at the momen...
- Niall FitzGibbon (20/23) Feb 10 2005 You have to actually call the function that's part of the template. My
- Stewart Gordon (17/34) Feb 10 2005 You mean
- Niall FitzGibbon (3/9) Feb 10 2005 That seems logical. I wasn't aware that the test!(typeof(dg))(dg) format...
- Ben Hinkle (2/38) Feb 10 2005 test!(typeof(dg))
- Kramer (5/46) Feb 10 2005 This compiles, but when run doesn't print anything. Plus, I just realiz...
- Ben Hinkle (23/31) Feb 10 2005 To call the function you need to explicitly call it:
- Russ Lewis (13/15) Feb 10 2005 Or here is an, IMHO, prettier template:
- Kramer (13/28) Feb 10 2005 This works great, thanks (and everyone else's examples)! I get excited ...
I'm trying to pass a delegate to a template but keep getting the error saying the template instance doesn't match any template declaration. Has anyone tried this and been successful? I've looked at the doc and it just says that templates accept type, value and alias type parameters; it doesn't explicitly exclude delegates though. Is this something that's going to be supported? Here's an example: Compiling the above gives: TemplateTest.d(9): template instance test!(dg) does not match any template declaration I've also tried specializing it, but still to no avail. :( Using DMD 0.111. -Kramer
Feb 09 2005
Kramer wrote:I'm trying to pass a delegate to a template but keep getting the error saying the template instance doesn't match any template declaration. Has anyone tried this and been successful? I've looked at the doc and it just says that templates accept type, value and alias type parameters; it doesn't explicitly exclude delegates though. Is this something that's going to be supported? Here's an example:It seems that the template itself is expecting a type parameter but you're trying to instantiate it with the instance of the delegate itself. As I understand it, test!(int delegate()) would be the correct form to instantiate that template. Maybe I'm missing something -- is D supposed to choose a type parameter from the type of a variable used in the instantiation? I don't remember reading anything to that effect in the D spec, but it might be a neat feature, if possible.
Feb 10 2005
Niall FitzGibbon wrote: <snip>It seems that the template itself is expecting a type parameter but you're trying to instantiate it with the instance of the delegate itself. As I understand it, test!(int delegate()) would be the correct form to instantiate that template.Correct.Maybe I'm missing something -- is D supposed to choose a type parameter from the type of a variable used in the instantiation?<snip> No. And even if it did, it would probably not be by the OP's syntax. Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Feb 10 2005
Compiles, but doesn't print. Maybe it's half-way supported at the moment? -Kramer In article <cufhh6$1pg8$1 digitaldaemon.com>, Stewart Gordon says...Niall FitzGibbon wrote: <snip>It seems that the template itself is expecting a type parameter but you're trying to instantiate it with the instance of the delegate itself. As I understand it, test!(int delegate()) would be the correct form to instantiate that template.Correct.Maybe I'm missing something -- is D supposed to choose a type parameter from the type of a variable used in the instantiation?<snip> No. And even if it did, it would probably not be by the OP's syntax. Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Feb 10 2005
Kramer wrote:Compiles, but doesn't print. Maybe it's half-way supported at the moment? -KramerYou have to actually call the function that's part of the template. My first guess at this was to do: test!(int delegate()).test(dg); or test!(typeof(dg)).test(dg); as Ben suggested. However, this lead to a compile error. I managed to get it to compile by changing the function name to be different to the template name. I'm not sure if this is a bug or intentional on the compiler's part, but renaming the function or template to be different from the other works: test!(typeof(dg)).testb(dg); template test(T) { void testb(T dg) { int i = dg(); writef("i = %d\n", i); } }
Feb 10 2005
Niall FitzGibbon wrote:Kramer wrote:You mean test!(int delegate()); compiles? That would seem a bug - I thought such blatant nop statements were meant to be illegal in D.Compiles, but doesn't print. Maybe it's half-way supported at the moment?You have to actually call the function that's part of the template.My first guess at this was to do: test!(int delegate()).test(dg); or test!(typeof(dg)).test(dg); as Ben suggested.My first guess would be test!(int delegate())(dg); or test!(typeof(dg))(dg);However, this lead to a compile error. I managed to get it to compile by changing the function name to be different to the template name.<snip> That suggests that when the only member of a template has the same name as the template itself, the only-name-once notation _must_ be used. Maybe this is to void ambiguities.... Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Feb 10 2005
That suggests that when the only member of a template has the same name as the template itself, the only-name-once notation _must_ be used. Maybe this is to void ambiguities.... Stewart.That seems logical. I wasn't aware that the test!(typeof(dg))(dg) format would work -- I was going by the TCopy/copy example in the template documentation :)
Feb 10 2005
In article <cufg4o$1nsi$1 digitaldaemon.com>, Niall FitzGibbon says...Kramer wrote:test!(typeof(dg))I'm trying to pass a delegate to a template but keep getting the error saying the template instance doesn't match any template declaration. Has anyone tried this and been successful? I've looked at the doc and it just says that templates accept type, value and alias type parameters; it doesn't explicitly exclude delegates though. Is this something that's going to be supported? Here's an example:It seems that the template itself is expecting a type parameter but you're trying to instantiate it with the instance of the delegate itself. As I understand it, test!(int delegate()) would be the correct form to instantiate that template. Maybe I'm missing something -- is D supposed to choose a type parameter from the type of a variable used in the instantiation? I don't remember reading anything to that effect in the D spec, but it might be a neat feature, if possible.
Feb 10 2005
In article <cufl90$1u3k$1 digitaldaemon.com>, Ben Hinkle says...In article <cufg4o$1nsi$1 digitaldaemon.com>, Niall FitzGibbon says...This compiles, but when run doesn't print anything. Plus, I just realized, I'd like to be able to templatize variable "i" with the return type of the template. I think I might be asking for too much... -KramerKramer wrote:test!(typeof(dg))I'm trying to pass a delegate to a template but keep getting the error saying the template instance doesn't match any template declaration. Has anyone tried this and been successful? I've looked at the doc and it just says that templates accept type, value and alias type parameters; it doesn't explicitly exclude delegates though. Is this something that's going to be supported? Here's an example:It seems that the template itself is expecting a type parameter but you're trying to instantiate it with the instance of the delegate itself. As I understand it, test!(int delegate()) would be the correct form to instantiate that template. Maybe I'm missing something -- is D supposed to choose a type parameter from the type of a variable used in the instantiation? I don't remember reading anything to that effect in the D spec, but it might be a neat feature, if possible.
Feb 10 2005
To call the function you need to explicitly call it: test!(typeof(dg))(dg); What's going on is the test!(typeof(dg)) just "instantiates" the template and doesn't do anything else. I'm exactly sure why you are using a template, though. You might want to consider using overloaded test functions: import std.stdio; void main() { int foo() { return 3; } test(&foo); } void test(int delegate() dg) { int i = dg(); writef("i = %d\n", i); } void test(double delegate() dg) { double x = dg(); writef("x = %g\n", x); }test!(typeof(dg))This compiles, but when run doesn't print anything. Plus, I just realized, I'd like to be able to templatize variable "i" with the return type of the template. I think I might be asking for too much...
Feb 10 2005
Ben Hinkle wrote:I'm exactly sure why you are using a template, though. You might want to consider using overloaded test functions:Or here is an, IMHO, prettier template: import std.stdio; void main() { int foo() { return 3; } test!(int)(&foo); } template test(T) void test(T delegate() dg) { writefln("i = ", dg()); }
Feb 10 2005
In article <cug9kk$2lkf$1 digitaldaemon.com>, Russ Lewis says...Ben Hinkle wrote:This works great, thanks (and everyone else's examples)! I get excited with some of D's features and don't always think them through before attempting use; that happens less though the more D I use. I really like this language and hope for D's success. :) Anyway, I do think it would be nice if you were able to extract the return type of a delegate/function. I.e.: test!(typeof(foo.return))(&foo) // something akin to this (or even going further, be able to extract a non-variadic functions parm types which could be resolved at compile-time) I just like genericity in general. I think it makes for more flexible programming IMO. -KramerI'm exactly sure why you are using a template, though. You might want to consider using overloaded test functions:Or here is an, IMHO, prettier template: import std.stdio; void main() { int foo() { return 3; } test!(int)(&foo); } template test(T) void test(T delegate() dg) { writefln("i = ", dg()); }
Feb 10 2005