Proposal: Implicit Function Template Value Instantiation

FeepingCreature feepingcreature at gmail.com
Wed Sep 28 12:59:08 UTC 2022


Everybody had to move from `writefln("%s = %s", a, b)` to 
`writefln!"%s = %s"(a, b)` in order to get the benefit of static 
format string syntax checking.

This was annoying. What's more, it was unnecessary. Why not just:

```
void writefln(string fmt, T...)(fmt, T args)
```

And then `writefln("%s = %s", a, b)` would be equivalent to 
`writefln!"%s = %s"(a, b)` automatically? In other words, the 
template value parameter would be inferred via the enum constant 
parameter value: IFTVI.

I tried to hack DMD to demo this, but I seem to have severely 
broken it. The compiler doesn't seem to like me *removing* a 
parameter from a function call during overloading. Unfortunately, 
I don't know enough about DMD to see what I did wrong, even in 
the extremely hacky code I threw together. 
https://gist.github.com/FeepingCreature/61ccf09d6e70e266aaa49a345dc76d23 is the patch, if someone wants to mess with it. It "works" to the extent that:

```
void print(string fmt, T...)(fmt, T args) {
   writefln!fmt(args);
}
```

seems to result in a *linker* error rather than a compiler error. 
This scared me enough that I've given up on touching it though. :)

Still, I think this would make templates more powerful while also 
somewhat normalizing their syntax: make function templates more a 
concern of the function writer than the function caller.


More information about the Digitalmars-d mailing list