Thread overview | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 13, 2005 Compile-time variadic functions | ||||
---|---|---|---|---|
| ||||
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. |
September 13, 2005 Re: Compile-time variadic functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to AJG | "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? |
September 15, 2005 Re: Compile-time variadic functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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.
|
September 15, 2005 Re: Compile-time variadic functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to AJG | "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. |
September 15, 2005 Re: Compile-time variadic functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley wrote:
> "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.
>
>
Maybe they should be treated just like templates!?
|
September 15, 2005 Re: Compile-time variadic functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hasan Aljudy | "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. |
September 15, 2005 Re: Compile-time variadic functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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... >> 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. 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. Sean |
September 15, 2005 Re: Compile-time variadic functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | Sean Kelly wrote: > 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... >> >>>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. > > > 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. > > > Sean > > 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 ) # somefunction( va_arg!(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. |
September 17, 2005 Re: Compile-time variadic functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley wrote:
> "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.
Yes, exactly. And not just inefficient, but limiting too; variadic functions could be much more robust if checked at compile-time.
Cheers,
--AJG.
|
September 17, 2005 Re: Compile-time variadic functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hasan Aljudy | Hasan Aljudy wrote: > Sean Kelly wrote: > >> 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... >>> >>>> 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. >> >> >> >> 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. >> >> >> Sean >> >> > > 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 ) > # somefunction( va_arg!(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. Yep. The possibilities are endless. > 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. |
Copyright © 1999-2021 by the D Language Foundation