I need to generate a space-padded string with a variable string length. The not-so-clever solution that works involved nesting of format
:
(format nil (format nil "~~~d,,a" 10) "asdf")
Now, I wanted to make it a bit more clever by using format
's ~?
for recursive processing. I would expect that something like this should do what I want:
(format nil "~@?" "~~~d,,a" 10 "asdf")
but what I get is just the formatting string, i.e. ~10,,a
, not the padded asdf
string. Perhaps I misunderstood the word 'recursive' here, but I would expect that having formed the inner format string, CL should proceed to actually use it. Am I missing something?
Variable arguments to format directives
FORMAT
allows you to usev
as an argument to a directive in the control string to pop arguments from the argument list.There may be multiple
v
s used.Recursive processing with
~?
The recursive processing directive is meant for embedding a different "call" to
FORMAT
inside a control string. For example, a function to prompt for a yes or no answer might be implemented with it.The caller can now format a prompt with this as they would with
FORMAT
without having to worry about the details of what the prompt should look like to the user (adding a new line at the beginning or the[y/n]:
prompt at the end).The result of
~?
will not be processed byFORMAT
, so it cannot be used to build a control string. In general, building control strings at run time is a bad idea, because it's error prone (for example, you must escape any unwanted tildes in the string) and prevents the implementation from processing the control string at compile time.