emacs-devel
[Top][All Lists]
Advanced

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

Re: RFC: String interpolation


From: Clément Pit--Claudel
Subject: Re: RFC: String interpolation
Date: Thu, 8 Dec 2016 13:37:43 -0500
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.5.1

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}"))

> 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 :)
This was inspired from Python's per-type format string, but I'm open to better 
syntax suggestions :)

> 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?

> So, summarized:
> 
> - %<expr> : format the value of <expr> where <expr> can be a  symbol
> or an s-expr, possibly also a vector or a string  (somewhat
> superfluously...)
>
> - %<fspec>`<expr> : format <expr> according to <fspec>.
> 
> Of course, It might be easier to use a different symbol for %, I
> don't know. I have nothing against using $.

Thanks, that's a nice syntax too.  I used $ based on a quick review of 
https://en.wikipedia.org/wiki/String_interpolation . 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.

>> 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.

Cheers and thanks for the feedback!
Clément.

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

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