Request for help: Use cases for static foreach and variadic templates

Olivier FAURE couteaubleu at gmail.com
Sat May 23 12:27:23 UTC 2020


I'm currently working on a RFC for the Rust programming language, 
to add variadic templates to the language. This is a 
poorly-understood topic, and previous proposals on the subject 
have been ad-hock, and lacked research work to justify why their 
preferred implementation was best.

I think the D community would be a good source of research, both 
because the language has powerful templates, and because 
variadics and static foreach have been around long enough to be 
"field-tested", which hopefully means people who used them will 
have a good understanding of the trade-offs of their design from 
a practical perspective, not just an academic one. Hence my 
question:

Could you give me examples of the most popular/polished/powerful 
libraries using variadic templates?

Github links pointing to specific areas of code would be best. 
Essentially, I'm looking for practical examples of real code 
using this feature, with a focus on the following questions:

- What are practical use cases where variadics are needed?

- What are some non-trivial use cases of variadics? A trivial use 
case would be "print takes a list of argument and prints each 
argument one by one". A non-trivial use case would be "turn a 
tuple of arrays into an array of tuples".

- How good are the compile errors? How often do you get an error 
which requires reading the internal code of template functions to 
understand what went wrong?

- What are some guidelines to respect when writing variadic 
functions and foreach code?

- Are there corner cases? Problems which often trip up first-time 
users of variadics, design problems that you'd think would be 
easy to implement but turn out to bump into the limitations of 
D's variadics implementation?

The reason I ask these questions is because Rust has a fairly 
different approach from D with regards to templates; in 
particular, it tries to have no "monomorphization errors", errors 
which are invisible when writing your template function, but 
appear when you call that function from somewhere else.

Obviously, avoiding monomorphization errors is a lot trickier 
with variadics, so I'd like help identifying the corner cases, 
with real-world examples of D code to analyze.


More information about the Digitalmars-d mailing list