emacs-devel
[Top][All Lists]
Advanced

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

Re: RFC: String interpolation


From: Lars Ingebrigtsen
Subject: Re: RFC: String interpolation
Date: Sat, 10 Dec 2016 16:39:00 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux)

Clément Pit--Claudel <address@hidden> writes:

>>   (let ((pvar (format "%.2f" variable)))
>>     "This number is ${pvar}s"
>> 
>> (You see the equivalent type of code in all languages that have "simple"
>> string interpolation, like PHP, all the time.)
>
> I thought I adressed this case specifically in my proposal :/
>
>   (fmt "This number is $[%.2f]{pvar}s")

Sure, you can add special cases to your domain-specific language to do
any formatting you want to.  Do you have a $[%] code for "strip out all
the blanks from a string"?  :-)

The problem with string interpolation is, in general, that it isn't very
general.  And it isn't "composable": Once you hit a case that isn't
covered, you have to rewrite the entire expression to a different form
(i.e., traditional format strings with arguments afterwards), and I know
from experience that people are loath to do that.  Instead the temporary
variables start popping up, obfuscating the code.

So...  I'm kinda on the "meh" side of the string interpolation debate.

As Ted alluded to, there's a different, template based method that I
think is more promising and is growing more popular in newer languages.
In Emacs Lisp, you'd have either have a macro like

(with-format ((zot "foo")
              (thing (funcall '+ 3.42423 4)))
  "This is %{thing}.2f and %{zot}ss")

or

(fmt "This is %{thing}.2f and %{zot}ss"
     zot
     (thing (funcall '+ 3.42423 4)))

That is, named format arguments with format specs.  You get all the
advantages of normal format (i.e., the parameters are real code, not
weirdly formatted things inside a string), but you don't have to match
up the order of the parameters to the order of the % directives.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no



reply via email to

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