www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Compile Time versus Run Time

reply Martin Tschierschke <mt smartdolphin.de> writes:
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.
Jul 31 2017
next sibling parent reply inevzxui <inevzxui inevzxui.ui> writes:
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.
Jul 31 2017
parent reply Anonymouse <asdf asdf.net> writes:
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 }
Jul 31 2017
parent Martin Tschierschke <mt smartdolphin.de> writes:
On Monday, 31 July 2017 at 15:57:28 UTC, Anonymouse wrote:
 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:
[...]
 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
 }
This is exactly the "use case", I thought about: How to avoid runtime errors, if at compile time the problem might be detected.
Jul 31 2017
prev sibling next sibling parent ag0aep6g <anonymous example.com> writes:
On 07/31/2017 05:43 PM, 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?
Kinda-sorta, but not really and you have to use the template syntax: ---- string format(alias fmt, A ...)(A args) { import std.format: sformat = format; static if (__traits(compiles, { enum e = fmt; })) return sformat!("(CT) " ~ fmt)(args); else return sformat("(RT) " ~ fmt, args); } void main() { import std.stdio: readln, writeln; // CT writeln(format!"Print this %s"(42)); // RT auto fmt = readln(); writeln(format!fmt(42)); // Can't call it like this: format!(readln())(42); // also still RT auto fmt2 = "Print this %s"; writeln(format!fmt2(42)); // (somewhat surprisingly) CT immutable fmt3 = "Print this %s"; writeln(format!fmt3(42)); } ----
Jul 31 2017
prev sibling parent Marco Leise <Marco.Leise gmx.de> writes:
Am Mon, 31 Jul 2017 15:43:21 +0000
schrieb Martin Tschierschke <mt smartdolphin.de>:

 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?
I see no way to accomplish this. For the compiler to see the contents of the format string it needs to be a template argument and as soon as you want to also allow runtime values to be accepted there, you need to use an alias, which in turn precludes the use of function results or concatenation. Believe me I've spent quite some time on trying something like this for format strings.
 Regards mt.
As far as using template arguments for code optimizations go, I know that at least GCC will turn runtime arguments into template arguments of sorts internally, thereby creating duplicates of the function with one or more arguments optimized out. On the other hand, you don't want to drive this too far. While it is nice to have compile-time checks, templates are actually troublesome on some levels. For example, the duplicated code makes it hard to cache the formatting function in the CPU and when writing libraries you always have to provide the full implementation that will get linked into the host application, which is a concern under certain licensing schemes. The benefits of shared libraries, like loading the code into memory once and use it by multiple processes or fixing security issues in one central place and have all programs use the new code without recompilation are also void. I.e. without template arguments, `format()` and all its dependencies (templates as well as regular functions) are compiled right into the Phobos shared library for all programs to use. If there is a security issue, it can be replaced with a patched version. Now with template arguments, `format!()` is compiled into each Dlang application multiple times for each format string and security fixes cannot be applied without recompiling them all. -- Marco
Jul 31 2017