digitalmars.D - Proposal: Implicit Function Template Value Instantiation
- FeepingCreature (27/27) Sep 28 2022 Everybody had to move from `writefln("%s = %s", a, b)` to
- jmh530 (9/18) Sep 28 2022 That's an interesting idea, but what would be the advantage of
- FeepingCreature (6/13) Sep 28 2022 Oh yeah! Enum parameters seem to come down to the same thing.
- Quirin Schroll (9/25) Sep 28 2022 Nice to hear. I’ll take that as a compliment.
- Steven Schveighoffer (7/10) Sep 28 2022 "had to" is a strong term. I didn't and don't ever plan to.
- Quirin Schroll (7/17) Sep 28 2022 Because of this post, the DIP draft now suggests a way to use
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.
Sep 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
Sep 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.orgOh 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.
Sep 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:Nice to hear. I’ll take that as a compliment.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.orgOh 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.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?
Sep 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
Sep 28 2022
On Wednesday, 28 September 2022 at 14:00:51 UTC, Steven Schveighoffer wrote:On 9/28/22 8:59 AM, FeepingCreature wrote: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.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.
Sep 28 2022