compile-time explicitness
Christophe
travert at phare.normalesup.org
Mon Sep 26 01:42:36 PDT 2011
Gor F. Gyolchanyan , dans le message (digitalmars.D:145120), a écrit :
> Template functions will have a very convenient syntax sugar:
>
> void myFunc(int arg1, compiletime int arg2, char arg3, compiletime char arg4)
> {
> }
>
> , which is equivalent to:
>
> vodi myFunc(int arg2, int arg3)(int arg1, int arg4)
> {
> }
> The function parameters can be marked as auto compiletime:
>
> void f(auto compiletime arg)
> {
> static if(__traits(compiletime, arg))
> {
> // partially specialize the function right in the scope.
> }
> }
That sounds really, really great to me. I often wanted to do something
like that. One of the strong point in D is CTFE, let's get even better
at this game.
auto compiletime seems to be much better that compiletime:
In many cases, the compiler will be able to optimize if like static if
without having to write the specialized function at all. When a
specialized function has to be rewritten, it should be IHMO in the same
function body. One of the issue with templating a specialized function
is also that it has to be separated from the function body.
My advice would be to have compiletime be what you described as auto
compiletime by default. The programmer could be allowed to prevent/not
recommend the compilation of the runtime version of function with a
static if/pragma.
BTW, the behavior of auto args with regards to function overloads and
other templates have to be precised.
> All this (almost) does not break any code!!! The almost stands for the keyword
> itself, which may be annotated @compiletime (like @safe) until D3, where the
> annotation will either be removed or, everything else will get annotated too.
Yes, this feature can be easily tested without breaking anything.
About other usages of the keyword:
- using enums for compiletime is nice, but it seems to me to be a hack
(we often use enum when we want the variable to be compile-time because
enum happens to be compile-time, but not because we want enum).
- for this, and because of the usage in function arguments, using the
compiletime keyword as variable attribute makes the language more
consistent.
- compiletime as function attribute to prevent using the function in
runtime is weird. Why would we want to do that ? However, this could be
an alternative to making all parameters compiletime, after
compiletime parameters are implemented.
> Do i deserve a candy for this or not? :-)
I think so :)
--
Christophe
More information about the Digitalmars-d
mailing list