www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Introducing Implicit Function Template Value Instantiation - safe

There are currently two ways to call every format function - a 
"safe" way and an "unsafe" way.

format!"%s and %s"("this", "that");
format("%s and %s", "this", "that");

Wouldn't it be cool if you could get the same syntax for both, 
using only a straightforward extension of a feature that already 
exists?

You can define templates like so: void foo(T)(T t)

And if you call foo(5), foo will be implicitly instantiated with 
int via IFTI.

Templates in D can take values as parameters. So why not:

void foo(int VALUE)(VALUE) { }

or for a less radical syntax

void foo(int VALUE : value)(int value) { }

So if you call foo(5), it will implicitly call foo!(5)(5) because 
VALUE is inferred to be 5.

So we could have string format(string fmt, T...)(fmt, T args) {} 
and if you called format("%s and %s", this, that) it would 
implicitly instantiate format as format!("%s and %s")("%s and 
%s", this, that) and allow format string checking for type safety.

This may also improve type safety in the format string DIP.
Feb 05 2020