Yet another terrible compile time argument proposal

Nickolay Bukreyev buknik95 at ya.ru
Sun Jan 14 21:34:30 UTC 2024


You’ve created this thread a minute before I was going to post a 
reply. :)

I came up with a different solution.

```d
db.execi!i"SELECT $x, $(x + y)";
// Lowers to:
db.execi!(
     Interpolation(["SELECT ", "x", ", ", "x + y", ""]), () => x, 
() => x + y,
);
```

Pros:
* Does not complicate overload resolution.
* Does not magically translate runtime arguments to compile time.
* Does not require changes to the language (besides the parser, 
to support istrings).
* Can work with Steven’s `Interpolation`, DIP1036, or whatever 
else.
* Supports UFCS.
* Produces clean assembly (with LDC).

Cons:
* Does not support nested istrings.
* Requires a string mixin (but we can provide it in the library).

```d
import std.algorithm.iteration: map;
import std.array: join;
import std.conv: text;
import std.range: iota;

enum invokeAll(string name, size_t n) =
     iota(n).map!(i => text(name ~ '[', i, "](),")).join();

void execImpl(Interpolation interp, Args...)(Sqlite db, Args 
args) {
     import std.stdio;

     enum query = generateSql(interp);
     // Just an example.
     writeln(query);
     static foreach (i, arg; args)
         writeln('?', i + 1, " = ", arg);
}

void execi(Interpolation interp, args...)(Sqlite db) {
     mixin(`db.execImpl!interp(`, invokeAll!(`args`, args.length), 
`);`);
}
```

(`generateSql` can be obtained 
[here](https://forum.dlang.org/post/jgniissqzeybvmkdorlk@forum.dlang.org), section _Proposal 2_.)

Any feedback will be welcome.


More information about the Digitalmars-d mailing list