Kaz Kylheku
1/4/2016 4:34:00 AM
On 2016-01-03, Sebastian Christ <rudolfo.christ@gmail.com> wrote:
> Hello c.l.l,
>
> I'm stuck with my control string. What I want is:
>
> (format t CONTROL-STRING nil "foo") ;=> No Scope - Name: foo
> (format t CONTROL-STRING "baz" "foo") ;=> Scope: baz - Name: foo
>
> What I've tried was
>
> (format t "~:[No Scope~:Scope: ~A~] - Name: ~A" scope name)
> but then the conditional consumes SCOPE, uses NAME for "Scope: ~A" and
> then there is no argument left for the "Name: ~A" part. Other
> experiments with @ and # modifiers were unsuccessful as well. :(
>
> Perhaps there is a simpler solution, but I'm not seeing it yet.
There is always KISS:
(defun scope-str (scope)
(if scope
(format nil "Scope: ~a" scope)
"No Scope"))
(format t "~a - Name ~a" (scope-str scope) name)
Don't like the accumulation of a string? We can easily turn this into a
function usable with format's tilde slash:
(defun fmt-scope (stream scope at-p colon-p)
(if scope
(format stream "Scope: ~a" scope)
(write-string "No Scope" stream)))
Usage:
[2]> (format t "~/fmt-scope/ - Name ~a~%" 'foo 'foo)
Scope: FOO - Name FOO
NIL
[3]> (format t "~/fmt-scope/ - Name ~a~%" nil 'foo)
No Scope - Name FOO
The above will still be obvious at a glance 17 months after
*not* looking at it.