Compile Time Argument Evaluation

DProgrammer2 register102 at snet.net
Thu Jan 3 17:43:05 PST 2013


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


More information about the Digitalmars-d-learn mailing list