Compile Time versus Run Time

Anonymouse via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 31 08:57:28 PDT 2017


On Monday, 31 July 2017 at 15:46:47 UTC, inevzxui wrote:
> On Monday, 31 July 2017 at 15:43:21 UTC, Martin Tschierschke 
> wrote:
>> As a rookie in D programming I try to understand the power of 
>> templated functions with compile time parameters. With DMD 
>> 2.074 a compile time format
>> (auto output = format!("Print this %s")(var);)
>>
>> was introduced, now we all know that very many of this format 
>> strings are immutable, so wouldn't it be cool to automatically 
>> detect this and use the compile time version?
>>
>> Without the need to think about it and to use an other syntax?
>> Is this theoretically possible?
>>
>> Regards mt.
>
> That's what writeln() does. The format is detected for each 
> element of the variadic.

But the parameters are not checked at compile-time unless you 
specifically pass the pattern string as a template parameter. I 
think its immutability implicitly converting it into a template 
parameter is what's what he's talking about.

import std.stdio;

void main(string[] args)
{
     writefln!"%s"(); // compile-time assert
     writefln("%s");  // runtime exception, though everything 
needed for a compile-time assert was inferable during compilation
}


More information about the Digitalmars-d-learn mailing list