digitalmars.D.learn - Compile Time Argument Evaluation
- DProgrammer2 (25/25) Jan 03 2013 Hello,
- bearophile (12/19) Jan 03 2013 I think currently there is no other way. Despite it's something
- Philippe Sigaud (7/13) Jan 04 2013 You can find a partial version in a template tutorial I put on github.
Hello, I was wondering if there would be some way to verify arguments to a function at compile time, but at runtime call only one function (in D2). To clarify what I mean: Say you have something like printf() printf(const char[] format, ...) I want to be able to make sure the arguments passed into the varadic part match what the format string says at compile time. I could do this with a template, but then it generates a new printf() function for each combination of arguments, and I'm trying to avoid that. It might be possible to solve this with a mixin as well, but I'd like to avoid writing: mixin(SomeFunc!("args", t1, t2, t3)) (Maybe alias would be useful? My D skills aren't that great yet) I would like it to be more like a regular function call SomeFunc("format", t1, t2) // Or something similar If there's no good way to do this that's fine. I bring this up because I saw https://github.com/xomboverlord/xomb/blob/unborn/kernel/core/kprintf.d which was neat, but it generates a new kprintf function for different arguments. I thought it'd be interesting to try and have the benefits of compile time checking without the extra generated code. Thank you
Jan 03 2013
DProgrammer2:If there's no good way to do this that's fine. I bring this up because I saw https://github.com/xomboverlord/xomb/blob/unborn/kernel/core/kprintf.d which was neat, but it generates a new kprintf function for different arguments. I thought it'd be interesting to try and have the benefits of compile time checking without the extra generated code.I think currently there is no other way. Despite it's something I'd like a lot. Someday I'd like a function like this in Phobos, that verifies the formatting string at compile-time: ctwritefln!"%d %s"(5, "hello"); Such function can be written with just a very light wrapper around a normal writefln (and later to something more optimized). This means that the actual template bloat can be minimized a lot, but not fully removed. Bye, bearophile
Jan 03 2013
On Fri, Jan 4, 2013 at 2:59 AM, bearophile <bearophileHUGS lycos.com> wrote:You can find a partial version in a template tutorial I put on github. https://github.com/PhilippeSigaud/D-templates-tutorial/ Select either of the .md, .pdf, .html or .epub version of the tutorial. It's section 5.11 "Statically-Checked Writeln". The code worked the last time I launched the automated extractor/tester (before 2.061).Someday I'd like a function like this in Phobos, that verifies theformatting string at compile-time: ctwritefln!"%d %s"(5, "hello"); Such function can be written with just a very light wrapper around a normal writefln (and later to something more optimized). This means that the actual template bloat can be minimized a lot, but not fully removed.
Jan 04 2013