enum Format

Walter Bright newshound2 at digitalmars.com
Wed Jan 10 19:53:48 UTC 2024


On 1/9/2024 2:38 PM, Timon Gehr wrote:
 > %s7 8 9

Yes, I used writeln instead of writefln. The similarity between the two names is 
a source of error, but if that was a festering problem we'd have seen a lot of 
complaints about it by now.


> And you can get rid of the runtime overhead by adding a `pragma(inline, true)` 
> `writeln` overload. (I guess with DMD that will still bloat the executable,

Try it and see.

I didn't mention the other kind of bloat - the rather massive number and size of 
template names being generated that go into the object file, as well as all the 
uncalled functions generated only to be removed by the linker.

As far as I can tell, the only advantage of DIP1036 is the use of inserted 
templates to "key" the tuples to specific functions. Isn't that what the type 
system is supposed to do? Maybe the real issue is that a format string should be 
a different type than a conventional string. For example:

```d
extern (C) pragma(printf) int printf(const(char*), ...);

enum Format : string;

void foo(Format f) { printf("Format %s\n", f.ptr); }
void foo(string s) { printf("string %s\n", s.ptr); }

void main()
{
     Format f = cast(Format)"f";
     foo(f);
     string s = "s";
     foo(s);
}
```
which prints:

Format f
string s

If we comment out `foo(string s)`:

test2.d(14): Error: function `test2.foo(Format f)` is not callable using 
argument types `(string)`
test2.d(14):        cannot pass argument `s` of type `string` to parameter 
`Format f`

If we comment out `foo(Format s)`:

string f
string s

This means that if execi()'s first parameter is of type `Format`, and the 
istring generates the format string with type `Format`, this key will fit the 
lock. A string generated by other means, such as `.text`, will not fit that lock.



More information about the Digitalmars-d mailing list