It seems pure ain't so pure after all
Steven Schveighoffer
schveiguy at yahoo.com
Mon Oct 1 13:50:53 PDT 2012
On Mon, 01 Oct 2012 16:23:17 -0400, Tommi <tommitissari at hotmail.com> wrote:
> On Monday, 1 October 2012 at 20:16:09 UTC, Steven Schveighoffer wrote:
>> 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
>
> Yes, I know. And this helps even more:
>
> template ct(alias expr)
> {
> enum ct = expr;
> }
>
> auto fun(string s)
> {
> ...
> }
>
> // usage:
>
> ct!(fun("times"))
>
> But that's still a nuisance.
Hm... I was focusing on changing fun("times") into fun!("times") in order
to make it always execute at compile time. I think that is not so bad.
The idea is that _funimpl is implementation details for fun, and doesn't
ever expect to be called at runtime. In fact, you could do if(!_ctfe)
throw Error();
-Steve
More information about the Digitalmars-d
mailing list