emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: RFC: String interpolation


From: Joost Kremers
Subject: Re: RFC: String interpolation
Date: Fri, 09 Dec 2016 09:57:14 +0100
User-agent: mu4e 0.9.18; emacs 25.1.50.3


On Thu, Dec 08 2016, Clément Pit--Claudel wrote:
On 2016-12-08 09:43, Joost Kremers wrote:
(let ((password "letmein")) (fmt "The password is ${password}")) ⇒
"The password is letmein"

Why the {}? Is there any context in which $<expr> is ambiguous such that ${<expr>} is needed to resolve it? Other than vectors, I mean

Yes, unfortunately: you can include virtually anything in a Lisp variable name.

(let* ((a 1)
       (b 2.0)
       (a/b (/ a b)))
  (fmt "$a/$b = ${a/b}"))

My initial thinking was that you could simply take everything up to the next whitespace character to be part of the symbol, but obviously I didn't realise that it should be possible *not* to have whitespace immediately after the value of a symbol. But I see your point now.

I honestly must say I find these last two very confusing. I would
simply write:

(fmt "Today is $(format-time-string \"%Y-%m-%d\" current-time)).")

The escaped double quotes are not perfect, but I find this much more
readable and more pleasant to look at.

I really dislike the escaped quotes, but it might just be me :)

Oh, I'm sure you're not the only one. :-) I'm not a big fan, either, but I can live with them. It'd be nice to have two different string delimiters, as in Python (actually, Python has three...) but that's not something a package can fix.

Anyway, your current implementation handles "$[date|%Y-%m-%d](current-time)" as well as the format that I prefer, so no-one loses out.

This was inspired from Python's per-type format string, but I'm open to better syntax suggestions :)

I was hoping to provide some, but looking at it now, I doubt I was... :-)

Perhaps you should also look at abo-abo's hydra package, since he does basically the same thing in his hydra doc strings. AFAICT, it works this way: % starts a placeholder, ` marks the start of the expression. If the expression is an s-expr, you can leave out the `.

Thanks for the reference! Do you know how it knows where to stop after a backtick?

From a quick look at the code, I believe the assumption is that
symbols only contain lower and uppercase letters, digits and the characters - and / . So that won't work. The relevant code seems to be here:

https://github.com/abo-abo/hydra/blob/master/hydra.el#L636

I'm a bit worried about the lack of delimiter after `, though;
maybe it would help to see what the example above (with $a/$b and $a/b) would look like in that syntax.

AFAICT it won't work. "%`a/b" would refer to the symbol `a/b', which is fine, but "%`a/%`b" would refer to a symbol `a/' and a symbol `b', but the former doesn't exist. And `a/b' only happens to work because / is included in the regexp used to parse hydra doc strings. Other characters won't work at all.

fmt expands the strings at compile time (so they must be constant
strings).

I've occasionally appreciated the fact that I can pass a dynamically
created format string to `format'... ;-) Ok, I could always use
`format' in such cases.

Yes; otherwise, fmt becomes a thinly veiled eval, and that isn't great.

True.

--
Joost Kremers
Life has its moments



reply via email to

[Prev in Thread] Current Thread [Next in Thread]