digitalmars.D - Compile-time variadic functions
- AJG (22/22) Sep 12 2005 Hi there,
- Jarrett Billingsley (25/47) Sep 13 2005 I'm not sure if this is what you're looking for, but you can overload
- AJG (9/38) Sep 14 2005 Hey, that's pretty nice. I didn't know the compiler chose the
- Jarrett Billingsley (5/9) Sep 15 2005 I see what you mean now. It sure is inefficient to have to check the ty...
- Hasan Aljudy (2/16) Sep 15 2005 Maybe they should be treated just like templates!?
- Jarrett Billingsley (5/6) Sep 15 2005 You're probably right. Variadic functions can be treated as another for...
- Sean Kelly (5/11) Sep 15 2005 The only catch here is that this would require implicit template instant...
- Hasan Aljudy (14/34) Sep 15 2005 Well, to be honest, I don't know much about templates, and I don't use
- AJG (4/47) Sep 16 2005 That's more or less exactly right. ;)
- AJG (5/17) Sep 16 2005 Yes, exactly. And not just inefficient, but limiting too; variadic
- Stewart Gordon (16/24) Sep 19 2005 Why would you want to truncate floats but pass all other types through
- AJG (15/38) Sep 19 2005 No, the example was trivial and not intended to do anything sensible.
Hi there, It seems to me that the entire variadic function process could run at compile-time. That is to say, the compiler has all the information required to make this happen. In other words, correct me if I'm wrong, but there is no way to call a function with a dynamically built variadic part. Right? If my assumption is correct, then why is the variadic mechanism a run-time one? In my opinion, as it stands, we are not getting the cake nor eating it. Were the process entirely compile-time, then we could make our variadic functions much more robust, and have the compiler trigger the errors, instead of the runtime. Wouldn't that be much better? For example, why couldn't I do something like the following: void Foo(...) in { static assert(_arguments.length > 1); static if (_arguments[0] == typeid(float)) { pragma(msg, "Warning, float will be truncated to int."); } } Thoughts? Cheers, --AJG.
Sep 12 2005
"AJG" <AJG nospam.com> wrote in message news:dg5ikk$e6s$1 digitaldaemon.com...Hi there, It seems to me that the entire variadic function process could run at compile-time. That is to say, the compiler has all the information required to make this happen. In other words, correct me if I'm wrong, but there is no way to call a function with a dynamically built variadic part. Right? If my assumption is correct, then why is the variadic mechanism a run-time one? In my opinion, as it stands, we are not getting the cake nor eating it. Were the process entirely compile-time, then we could make our variadic functions much more robust, and have the compiler trigger the errors, instead of the runtime. Wouldn't that be much better? For example, why couldn't I do something like the following: void Foo(...) in { static assert(_arguments.length > 1); static if (_arguments[0] == typeid(float)) { pragma(msg, "Warning, float will be truncated to int."); } } Thoughts? Cheers, --AJG.I'm not sure if this is what you're looking for, but you can overload variadic functions, and the compiler will select the correct one, kind of like template specialization. For example: import std.stdio; void fork(...) { writefln("variadic fork."); } void fork(int x, int y) { writefln("specialized fork."); } void main() { fork(5); fork(5,6); fork(5,6,7); } Prints: variadic fork. specialized fork. variadic fork. Maybe it's useful to you?
Sep 13 2005
Hi,I'm not sure if this is what you're looking for, but you can overload variadic functions, and the compiler will select the correct one, kind of like template specialization. For example: import std.stdio; void fork(...) { writefln("variadic fork."); } void fork(int x, int y) { writefln("specialized fork."); } void main() { fork(5); fork(5,6); fork(5,6,7); } Prints: variadic fork. specialized fork. variadic fork.Hey, that's pretty nice. I didn't know the compiler chose the specialized one in such cases.Maybe it's useful to you?I bet it will. However, it doesn't quite address my original point: Namely, that the variadic function mechanism should occur completely at compile time. As it stands, it's done through a run-time process, which IMO is unnecessary. Thanks, --AJG.
Sep 14 2005
"AJG" <AJG nospam.com> wrote in message news:dgattf$2q0r$1 digitaldaemon.com...I bet it will. However, it doesn't quite address my original point: Namely, that the variadic function mechanism should occur completely at compile time. As it stands, it's done through a run-time process, which IMO is unnecessary.I see what you mean now. It sure is inefficient to have to check the types of the parameters every time the function is called at run-time, when it could be all pre-checked at compile time.
Sep 15 2005
Jarrett Billingsley wrote:"AJG" <AJG nospam.com> wrote in message news:dgattf$2q0r$1 digitaldaemon.com...Maybe they should be treated just like templates!?I bet it will. However, it doesn't quite address my original point: Namely, that the variadic function mechanism should occur completely at compile time. As it stands, it's done through a run-time process, which IMO is unnecessary.I see what you mean now. It sure is inefficient to have to check the types of the parameters every time the function is called at run-time, when it could be all pre-checked at compile time.
Sep 15 2005
"Hasan Aljudy" <hasan.aljudy gmail.com> wrote in message news:dgchlh$1tsh$1 digitaldaemon.com...Maybe they should be treated just like templates!?You're probably right. Variadic functions can be treated as another form of generic programming. And since they're compile-time functions, they'd fit right in.
Sep 15 2005
In article <dgcjfg$20ce$1 digitaldaemon.com>, Jarrett Billingsley says..."Hasan Aljudy" <hasan.aljudy gmail.com> wrote in message news:dgchlh$1tsh$1 digitaldaemon.com...The only catch here is that this would require implicit template instantiation, which I gather isn't a trivial addition. But I agree that this should be a template feature if it's implemented. SeanMaybe they should be treated just like templates!?You're probably right. Variadic functions can be treated as another form of generic programming. And since they're compile-time functions, they'd fit right in.
Sep 15 2005
Sean Kelly wrote:In article <dgcjfg$20ce$1 digitaldaemon.com>, Jarrett Billingsley says...Well, to be honest, I don't know much about templates, and I don't use them much either... but I was just reading the thread "writef / doFormat without formatting", and man, if you look at Dark Parnell's stringer code in that thread, you will that see he's doing a whole bunch of #if( typeid is X ) calling the same function for every single basic type. Then I thought, if variadic functions were compile-time, he could've just written #somefunction( va_arg!(typeof(T) ) or something like that. That's probably not only easier to code for the programmer, but also faster at runtime, because the runtime overhead is moved to compile-time."Hasan Aljudy" <hasan.aljudy gmail.com> wrote in message news:dgchlh$1tsh$1 digitaldaemon.com...The only catch here is that this would require implicit template instantiation, which I gather isn't a trivial addition. But I agree that this should be a template feature if it's implemented. SeanMaybe they should be treated just like templates!?You're probably right. Variadic functions can be treated as another form of generic programming. And since they're compile-time functions, they'd fit right in.
Sep 15 2005
Hasan Aljudy wrote:Sean Kelly wrote:Yep. The possibilities are endless.In article <dgcjfg$20ce$1 digitaldaemon.com>, Jarrett Billingsley says...Well, to be honest, I don't know much about templates, and I don't use them much either... but I was just reading the thread "writef / doFormat without formatting", and man, if you look at Dark Parnell's stringer code in that thread, you will that see he's doing a whole bunch of #if( typeid is X ) calling the same function for every single basic type. Then I thought, if variadic functions were compile-time, he could've just written #somefunction( va_arg!(typeof(T) ) or something like that."Hasan Aljudy" <hasan.aljudy gmail.com> wrote in message news:dgchlh$1tsh$1 digitaldaemon.com...The only catch here is that this would require implicit template instantiation, which I gather isn't a trivial addition. But I agree that this should be a template feature if it's implemented. SeanMaybe they should be treated just like templates!?You're probably right. Variadic functions can be treated as another form of generic programming. And since they're compile-time functions, they'd fit right in.That's probably not only easier to code for the programmer, but also faster at runtime, because the runtime overhead is moved to compile-time.That's more or less exactly right. ;) --AJG.
Sep 16 2005
Jarrett Billingsley wrote:"AJG" <AJG nospam.com> wrote in message news:dgattf$2q0r$1 digitaldaemon.com...Yes, exactly. And not just inefficient, but limiting too; variadic functions could be much more robust if checked at compile-time. Cheers, --AJG.I bet it will. However, it doesn't quite address my original point: Namely, that the variadic function mechanism should occur completely at compile time. As it stands, it's done through a run-time process, which IMO is unnecessary.I see what you mean now. It sure is inefficient to have to check the types of the parameters every time the function is called at run-time, when it could be all pre-checked at compile time.
Sep 16 2005
AJG wrote: <snip>For example, why couldn't I do something like the following: void Foo(...) in { static assert(_arguments.length > 1); static if (_arguments[0] == typeid(float)) { pragma(msg, "Warning, float will be truncated to int."); } }Why would you want to truncate floats but pass all other types through unchanged? If OTOH you want the first argument to be an int, then you would do void Foo(int bar, ...) { } Stewart. -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d- s:- C++ a->--- UB P+ L E W++ N+++ o K- w++ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++>++++ h-- r-- !y ------END GEEK CODE BLOCK------ My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Sep 19 2005
Hi, Stewart Gordon wrote:AJG wrote: <snip>No, the example was trivial and not intended to do anything sensible. The point was to show that variadic functions should be resolved at compile time so that static checks can be done. Right now, those checks must: a) Occur at runtime. b) Occur every time a function is called. This is completely unnecessary. Everything (AFAIK) needed to convert the process to compile-time is there. Functions cannot be called with a dynamically-built parameter list. Therefore, why treat it as if it were one? I think the solution lies with what the others suggested, i.e., some form of implicit templating. Cheers, --AJG.For example, why couldn't I do something like the following: void Foo(...) in { static assert(_arguments.length > 1); static if (_arguments[0] == typeid(float)) { pragma(msg, "Warning, float will be truncated to int."); } }Why would you want to truncate floats but pass all other types through unchanged? If OTOH you want the first argument to be an int, then you would do void Foo(int bar, ...) { } Stewart.
Sep 19 2005