Puzzle 8-10-08 (answer)

Koroskin Denis 2korden at gmail.com
Tue Aug 12 02:31:50 PDT 2008


On Tue, 12 Aug 2008 05:15:09 +0400, bearophile <bearophileHUGS at lycos.com>  
wrote:

> Koroskin Denis:
>>      return square!(a); // you can't instantiate a template
>>      // with a variable (unless it is an alias, which is not a case)
>
> Uhm... I think there can be ways to modify DMD to allow a better mixing  
> of CT functions and templates.
>
>

Yes, I think it can be asked as an enhancement. As a result this function  
becomes Compile-Time evalualable only, so it will yield "Can not evaluate  
at run-time" if used with non-constant parameters. I like that bridge  
between functions and templates and it would reduce code complexity.

>> That's because compiler doesn't have to instantiate all those hundreds  
>> of
>> templates (and hold them in memory until exit).
>
> Now the compile time is around 0.06 s, way less.
> Such difference is very interesting. A functional language (even an  
> interpreted one) can probably execute the code relative to those  
> templates in way less than the original 1.8 seconds (it amounts to  
> almost 4 billion clock ticks, it's a lot of computations!).
> I presume DMD somehow contains an almost complete interpreter of D  
> language (with GC) plus a functional language interpreter for the  
> templates that can probably be improved, plus the compiler itself, it  
> sounds like a lot of complexity/stuff :-)
>
>

Yes, it is. And Walter writes it on his own :(

>> Note that now the
>> result is returned as a string due to a lack of compile time text output
>> (it will be output once per template/fuction instance).
>
> Good idea, I'll remember it.
>
> Bye,
> bearophile


More information about the Digitalmars-d-learn mailing list