Thread overview
Proposal: Implicit Function Template Value Instantiation
Sep 28, 2022
FeepingCreature
Sep 28, 2022
jmh530
Sep 28, 2022
FeepingCreature
Sep 28, 2022
Quirin Schroll
Sep 28, 2022
Quirin Schroll
September 28, 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.

September 28, 2022

On Wednesday, 28 September 2022 at 12:59:08 UTC, FeepingCreature wrote:

>

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)

[snip]

That's an interesting idea, but what would be the advantage of this (or something like it) versus the Enum Parameters DIP [1]?

I'm also not so trilled with the fmt in the second set of parentheses not having a type associated with it. I don't know if that would cause your linking errors.

[1] https://forum.dlang.org/thread/gvypfkueypukgvxnfgtx@forum.dlang.org

September 28, 2022

On 9/28/22 8:59 AM, FeepingCreature wrote:

>

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.

"had to" is a strong term. I didn't and don't ever plan to.

I'm not interested in template bloat for something that works perfectly fine without it. I don't need the compiler to tell me that writefln("%s = %s", a, b) is correct.

And I certainly don't want the compiler to make this choice for me.

-Steve

September 28, 2022

On Wednesday, 28 September 2022 at 13:29:14 UTC, jmh530 wrote:

>

That's an interesting idea, but what would be the advantage of this (or something like it) versus the Enum Parameters DIP [1]?

I'm also not so trilled with the fmt in the second set of parentheses not having a type associated with it. I don't know if that would cause your linking errors.

[1] https://forum.dlang.org/thread/gvypfkueypukgvxnfgtx@forum.dlang.org

Oh yeah! Enum parameters seem to come down to the same thing. That makes sense! I think enum parameters look a lot better, too. An advantage of IFTVI would be that the template parameters for it could be used as specializations for other parameters, but that's very niche.

September 28, 2022
On Wednesday, 28 September 2022 at 14:42:35 UTC, FeepingCreature wrote:
> On Wednesday, 28 September 2022 at 13:29:14 UTC, jmh530 wrote:
>> That's an interesting idea, but what would be the advantage of this (or something like it) versus the Enum Parameters DIP [1]?
>>
>> I'm also not so trilled with the `fmt` in the second set of parentheses not having a type associated with it. I don't know if that would cause your linking errors.
>>
>> [1] https://forum.dlang.org/thread/gvypfkueypukgvxnfgtx@forum.dlang.org
>
> Oh yeah! Enum parameters seem to come down to the same thing. That makes sense! I think enum parameters look a lot better, too.

Nice to hear. I’ll take that as a compliment.

> An advantage of IFTVI would be that the template parameters for it could be used as specializations for other parameters, but that's very niche.

You mean like this?
```d
void f(string s, string t : s)(s, t) { }
```

If not could you explain it with a little more detail or an example?
September 28, 2022

On Wednesday, 28 September 2022 at 14:00:51 UTC, Steven Schveighoffer wrote:

>

On 9/28/22 8:59 AM, FeepingCreature wrote:

>

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.

"had to" is a strong term. I didn't and don't ever plan to.

I'm not interested in template bloat for something that works perfectly fine without it. I don't need the compiler to tell me that writefln("%s = %s", a, b) is correct.

And I certainly don't want the compiler to make this choice for me.

Because of this post, the DIP draft now suggests a way to use enum parameters without unnecessary template bloat: @nodbi (read: no design by introspection) effectively makes the parameter a run-time parameter, but allows checks in contracts and static asserts.