Renewcommand with ensuremath and brackets

I merely added {} in the \DEFapol macro in the part that went

...\ensuremath{#2{}[##1]}...

Here is the MWE:

\documentclass{article}

\usepackage{amsmath,amssymb,amsfonts,amsthm}

\newcommand{\apol}{}
\newcommand{\DEFapol}[2][X,Y]{\renewcommand{\apol}[1][#1]%
    {\ensuremath{#2{}[##1]}}}
\newcommand{\fq}[1][q]{\ensuremath{\mathbb{F}_{#1}}}

\begin{document}

\DEFapol[X]{K}

$\apol$

\DEFapol{K}

$\apol$

\DEFapol[X,Y,Z]{\fq}

$\apol$

\end{document}

enter image description here


Leaving aside \ensuremath that doesn't seem necessary (and seldom is), here's what happens with \DEFapol[X,Y,Z]{\fq}

\renewcommand{\apol}[1][X,Y,Z]{\fq[#1]}

When you now call \apol, you get

\fq[X,Y,Z]

and the definition of \fq produces X,Y,Z according to its definition.

What you want is something that avoids a possible misinterpretation of [ as the delimiter of an optional argument; the primitive \mathopen comes very handy, as it transforms [ into an Open atom, which it already is.

\renewcommand{\apol}[1][X,Y,Z]{{\fq}\mathopen[#1]}

Hence the code is

\newcommand{\apol}{}
\newcommand{\DEFapol}[2][X,Y]{%
  \renewcommand{\apol}[1][#1]{#2\mathopen[##1]}%
}
\newcommand{\fq}[1][q]{\mathbb{F}_{#1}}

On the other hand, I'm not sure this is really useful, because when you'll reread the typescript you would not remember what a particular \apol stands for.