all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Joost Kremers <joostkremers@fastmail.fm>
To: "Clément Pit--Claudel" <clement.pit@gmail.com>
Cc: Emacs developers <emacs-devel@gnu.org>
Subject: Re: RFC: String interpolation
Date: Thu, 08 Dec 2016 15:43:04 +0100	[thread overview]
Message-ID: <87r35iwkg7.fsf@fastmail.fm> (raw)
In-Reply-To: <51825111-ace4-f750-4077-026a3b648d27@gmail.com>


On Thu, Dec 08 2016, Clément Pit--Claudel wrote:
> Hi all,
>
> Many languages are gaining literal string interpolations 
> features of late (the latest example is Python; see 
> https://www.python.org/dev/peps/pep-0498/).  The TL;DR is that 
> in Python 3.6 one can write this:
>
>     value = 1.3
>     print(f'The value is {value + 1:.2f}.')
>
> This prints "The value is 2.30."  Plenty of other languages have 
> similar features.
>
> It would be nice if Emacs offered something similar out of the 
> box. It could be a reader macro, but a plain macro would do just 
> as well, and indeed it's relatively easy to code something up (I 
> attached an example implementation).

That's definitely something that would be great to have. I'm not 
sure I would have chosen the same syntax, though.

> With the attached fmt.el, you can write things like this:
>
>     (let ((a 12) (b 15))
>       (fmt "$a + $b = $(+ a b)"))
>     ⇒ "12 + 15 = 27"

That syntax looks great to me, and I don't really see any reason 
to make it much more complex.

>     (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, since you use [] for an optional format spec? Personally, 
I'd just say that vectors need to be written as (vector ...). 
[]-vectors are constants anyway, so in such cases you might as 
well write the string directly.

>     (let ((n 25) (price-per-unit 10.5))
>       (fmt "The price for $n units is €$[%.2f](* n 
>       price-per-unit)"))
>     ⇒ "The price for 25 units is €262.50"
> 
>     (fmt "Here's a symbol: '${'a}; "
>          "and a padded number: ‘$[%-6.3f]{1}’.")
>     ⇒ "Here's a symbol: 'a; and a padded number: ‘1.000 ’."
>
>     (fmt "Welcome to Emacs! "
>          "Press $[where-is]{'help-with-tutorial} to open the 
>          tutorial.")
>     ⇒ "Welcome to Emacs! Press C-h t to open the tutorial."
>
>     (fmt "Today is $[date|%Y-%m-%d](current-time).")
>     ⇒ "Today is 2016-12-07."

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.

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 
`. So your examples would be:


>     (let ((a 12) (b 15))
>       (fmt "$a + $b = $(+ a b)"))

  (fmt "%`a + %`b = %(+ a b)")

>     (let ((password "letmein"))
>       (fmt "The password is ${password}"))

  (fmt "The password is %`password")

>     (let ((n 25) (price-per-unit 10.5))
>       (fmt "The price for $n units is €$[%.2f](* n 
>       price-per-unit)"))

(fmt "The price for %`n units is €%.2f(* n price-per-unit)") 

>     (fmt "Here's a symbol: '${'a}; "
>          "and a padded number: ‘$[%-6.3f]{1}’.")

  (fmt "Here's a symbol: '%`'a; "
       "and a padded number: ‘%-6.3f`1’.")

(Though here you might as well forego fmt altogether).

>     (fmt "Welcome to Emacs! "
>          "Press $[where-is]{'help-with-tutorial} to open the 
>          tutorial.")

  (fmt "Welcome to Emacs! "
       "Press %(fmt-whereis 'help-with-tutorial) to open the 
       tutorial.")

This one would require `fmt-whereis' to be an alias for your 
`fmt--printers-whereis'.

>     (fmt "Today is $[date|%Y-%m-%d](current-time).")

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

One addition might be to let %n stand for the value of the symbol 
n, and only interpret what immediately follows % as a format spec 
when there is also a backtick. So

  (fmt "The value of x is: %x")

would refer to the value of the variable s, while

  (fmt "The value of x is: %x`x")

would format the value of x in hexadecimal.

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

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


-- 
Joost Kremers
Life has its moments



  parent reply	other threads:[~2016-12-08 14:43 UTC|newest]

Thread overview: 86+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-12-08  1:13 RFC: String interpolation Clément Pit--Claudel
2016-12-08  8:27 ` Andreas Schwab
2016-12-08  8:38   ` Joost Kremers
2016-12-08  9:05     ` Philippe Vaucher
2016-12-08 18:25   ` Clément Pit--Claudel
2016-12-08  9:02 ` Philippe Vaucher
2016-12-08 18:22   ` Clément Pit--Claudel
2016-12-08 14:43 ` Joost Kremers [this message]
2016-12-08 18:37   ` Clément Pit--Claudel
2016-12-08 20:04     ` Stefan Monnier
2016-12-09  8:57     ` Joost Kremers
2016-12-08 15:51 ` Drew Adams
2016-12-08 18:21   ` Clément Pit--Claudel
2016-12-08 20:38   ` Richard Stallman
     [not found]   ` <<E1cF5SK-0000HO-EL@fencepost.gnu.org>
2016-12-08 22:04     ` Drew Adams
2016-12-08 19:05 ` Stefan Monnier
2016-12-08 19:31   ` Clément Pit--Claudel
2016-12-08 21:31     ` Stefan Monnier
2016-12-10 16:01       ` Clément Pit--Claudel
2016-12-09 19:19 ` Ted Zlatanov
2016-12-09 22:45   ` Clément Pit--Claudel
2016-12-11  2:53     ` Ted Zlatanov
2016-12-11 18:46       ` Lars Ingebrigtsen
2016-12-11 18:56         ` Clément Pit--Claudel
2016-12-11 19:34           ` Lars Ingebrigtsen
2016-12-11 17:38     ` Stefan Monnier
2016-12-11 17:53       ` Clément Pit--Claudel
2016-12-10 14:11   ` Lars Ingebrigtsen
2016-12-10 14:25     ` Clément Pit--Claudel
2016-12-10 15:39       ` Lars Ingebrigtsen
2016-12-10 15:49         ` Clément Pit--Claudel
2016-12-10 16:01           ` Yuri Khan
2016-12-10 17:58             ` Clément Pit--Claudel
2016-12-11 17:47             ` Stefan Monnier
2016-12-11 19:31               ` Yuri Khan
2016-12-11 20:12                 ` Stefan Monnier
2016-12-11  9:35           ` Richard Stallman
2016-12-11 16:29             ` Clément Pit--Claudel
2016-12-11 18:42             ` Lars Ingebrigtsen
2016-12-12 19:41               ` Richard Stallman
2016-12-11 18:41           ` Lars Ingebrigtsen
2016-12-11 21:05             ` Helmut Eller
2016-12-11 21:26               ` Lars Ingebrigtsen
2016-12-12  2:39                 ` Stefan Monnier
2016-12-12  9:44                   ` Lars Ingebrigtsen
2016-12-12 13:03                     ` Stefan Monnier
2016-12-12 14:47                       ` Lars Ingebrigtsen
2016-12-12 15:04                         ` Stefan Monnier
2016-12-12 15:26                           ` Lars Ingebrigtsen
2016-12-12 17:05                             ` Stefan Monnier
2016-12-12 17:12                             ` Lars Ingebrigtsen
2016-12-12 17:23                               ` Stefan Monnier
2016-12-12 13:40                     ` Clément Pit--Claudel
2016-12-12 14:43                       ` Lars Ingebrigtsen
2016-12-12 14:54                         ` Clément Pit--Claudel
2016-12-12 17:14                           ` Stefan Monnier
2016-12-12 17:46                     ` Paul Eggert
2016-12-12 18:33                       ` Lars Ingebrigtsen
2016-12-12 19:51                       ` Ted Zlatanov
2016-12-12 20:41                         ` Paul Eggert
2016-12-12 23:40                           ` Paul Eggert
2016-12-13 17:42                             ` Richard Stallman
2016-12-13 18:05                               ` Paul Eggert
2016-12-13 23:55                                 ` Lars Ingebrigtsen
2016-12-14  0:07                                   ` Paul Eggert
2016-12-14  0:10                                     ` John Wiegley
2016-12-14  0:14                                       ` Lars Ingebrigtsen
2016-12-14  0:17                                       ` Paul Eggert
2016-12-14  0:26                                         ` John Wiegley
2016-12-14  1:48                                           ` Drew Adams
2016-12-14  2:48                                             ` Elias Mårtenson
2016-12-14  3:33                                               ` Drew Adams
2016-12-14  3:52                                                 ` Elias Mårtenson
2016-12-14 13:52                                             ` Lars Brinkhoff
2016-12-14 13:57                                               ` Lars Brinkhoff
2016-12-14  0:24                                       ` Drew Adams
2016-12-14  0:21                                     ` Clément Pit--Claudel
2016-12-14 14:10                                     ` Lars Ingebrigtsen
2016-12-14 17:00                                 ` Richard Stallman
2016-12-12 19:43                     ` Richard Stallman
2016-12-12 21:11                       ` Lars Ingebrigtsen
2016-12-13 17:41                         ` Richard Stallman
2016-12-13 17:43                           ` Lars Ingebrigtsen
2016-12-14 17:01                             ` Richard Stallman
2016-12-13  1:04                       ` Ted Zlatanov
2016-12-12 16:39                   ` Helmut Eller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87r35iwkg7.fsf@fastmail.fm \
    --to=joostkremers@fastmail.fm \
    --cc=clement.pit@gmail.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.