It seems pure ain't so pure after all

Steven Schveighoffer schveiguy at yahoo.com
Mon Oct 1 13:16:27 PDT 2012


On Mon, 01 Oct 2012 16:10:48 -0400, Tommi <tommitissari at hotmail.com> wrote:

> On Monday, 1 October 2012 at 18:36:23 UTC, Jonathan M Davis wrote:
>> CTFE was specifically designed with the idea that you would not need to  
>> mark functions as CTFEable. You can call _any_ functio
>> at compile time. Some will fail, because they're doing things that CTFE  
>> won't allow, but that's quickly caught, because it happens at compile  
>> time. It completely avoids needing to mark
>> functions as CTFEable all over the place (which would generally
>> mean that functions wouldn't be CTFEable, because people would
>> frequently not mark them as CTFEable). constexpr is in direct
>> conflict with that design goal.
>
> That's not what I suggested. I meant that all functions would still be  
> implicitly CTFEable by default, but an attribute like force_ctfe would  
> make it so that the function is guaranteed to execute at compile-time  
> when its arguments are compile-time constants.
>
>> And if you want a function to be executed at compile time, you assign  
>> its result to an enum or static variable. Done. It's easy
>> and straightforward. I really don't understand why this is an
>> issue at all.
>
> The issue to me is complicating the syntax of your code. The problem is  
> *having* to assign the result first to an enum, when I shouldn't have  
> to. I would like to be able to just say fun("times") and be confident  
> that that's going to be evaluated at compile-time. I feel like I've done  
> my part of the deal here, I provided the compile-time argument, and now  
> I'd expect the compiler to do his part and evaluate the function at  
> compile-time (if it has that force_ctfe attribute).

We already have that, use templates:

private auto _funimpl(string s) {
/* what was previously in fun(string) */
}

template fun(string s)
{
    enum fun = _funimpl(s);
}

// usage:

fun!("times"); // always executes _funimpl at compile time

-Steve


More information about the Digitalmars-d mailing list