The state of string interpolation

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Dec 7 03:44:16 UTC 2018


On Thu, Dec 06, 2018 at 10:29:11PM -0500, Steven Schveighoffer via Digitalmars-d wrote:
[...]
> vibe.d does string interpolation in it's diet templates, but the rub
> is that you have to pass in an alias to the variables you want to be
> visible in the interpolation. So it requires a very un-DRY solution,
> and also doesn't work with arbitrary expressions.

I've discovered that since alias parameters accept module symbols, I can
pass my module to a Diet template, and then the template has free access
to everything in the module. :-D  E.g.,

	// mymodule.d
	int x;
	int y;
	...
	render!("mytemplate.dt", mymodule);

	// mytemplate.dt
	html
	    body
	        p x=#{mymodule.x} y=#{mymodule.y}

If your render!"" call is inside a class, you can also pass in `this`,
then you can access all class members.


[...]
> As I said, there's already possible libraries that allow what we want.
> It's the usage syntax that is the pain-point I would say.
[...]

True. But we'll have to think of a convincing argument of why building
the syntax into the language is a good thing -- good enough to pass
W&A's scrutiny.  That's the difficult part.

There's also the matter of exactly what syntax we're going to use for
interpolated strings. Since it's going to be baked into the language,
any changes / tweaks will be a lot harder to make. So we have to get it
(almost) perfect on first try (or at least, a perfect subset that can be
extended later without breaking anything that depends on it), which is
not easy to achieve. There will probably be bikeshedding over the finer
details of syntax.


T

-- 
Тише едешь, дальше будешь.


More information about the Digitalmars-d mailing list