The state of string interpolation

H. S. Teoh hsteoh at quickfur.ath.cx
Sat Dec 8 02:49:46 UTC 2018


On Fri, Dec 07, 2018 at 09:29:10PM -0500, Steven Schveighoffer via Digitalmars-d wrote:
[...]
> But it's not an AliasSeq. It's simply a list of items.
> 
> for example, I want this to work:
> 
> writeln(i"a + b = ${a+b}");
> 
> And this compiles:
> 
> writeln("a + b = ", a + b);
> 
> But this does not:
> 
> writeln(AliasSeq!("a + b = ", a + b));
[...]

Hmph.  I forgot about this limitation of compile-time tuples aka
AliasSeq: you cannot capture expressions this way. :-(

The best I could come up with was to replicate std.typecons.Tuple:

	import std.stdio;
	alias AliasSeq(T...) = T;
	struct Tuple(T...)
	{
		T t;
		alias t expand;
	}
	auto func(Args...)(Args args)
	{
		return Tuple!Args(args);
	}
	void main() {
		int a=1, b=2;

		// Desired effect
		writeln("Sum of ", a, " and ", b, " is ", a+b);

		// NG: doesn't compile:
		//writeln(AliasSeq!("Sum of ", a, " and ", b, " is ", a+b));

		// This works (but is ugly)
		writeln(func("Sum of ", a, " and ", b, " is ", a+b).expand);
	}


This certainly puts a damper in our proposal. :-(

How does Marler's implementation behave? Does it capture expressions? If
it does, we have good news (but we'll have to document this clearly,
since it would be unprecedented in the current language).  If not, we'll
have to think more carefully about how to implement this.


T

-- 
Doubtless it is a good thing to have an open mind, but a truly open mind should be open at both ends, like the food-pipe, with the capacity for excretion as well as absorption. -- Northrop Frye


More information about the Digitalmars-d mailing list