[Tex/LaTex] lstinline loses braces when used as an argument to another command

braceslistingsmacros

I'm using the listings package, for the Java language (which, as all C-like languages, uses braces as a block delimiter). I intend to use \lstinline{...} as the second argument to a command for typesetting Hoare triples.

While \lstinline|while (...) { ... } | typesets the braces correctly, they are removed if \lstinline|...| is passed as argument to any macro. E.g.:

\documentclass{article}
\usepackage{listings}
\lstset{language=Java}

\newcommand{\hoare}[3]{\{ $#1$ \} #2 \{ $#3$ \} }
\newcommand{\true}{\mathbf{true}}
\newcommand{\false}{\mathbf{false}}

\begin{document}
\hoare{\true}{\lstinline|while(true) { }|}{\false}
\end{document}

How do I get my braces back?

Best Answer

You could quote the braces to make them visible:

\hoare{\true}{\lstinline|while(true) \{ \}|}{\false}

lstinline with braces

When directly called, \lstinline|while(true) \{ \}| would also print the backslash, but not here in the macro argument, because the outer macro already deals with the quoted braces.