Indirectly defined macros: Undefined macro with "@" does not trigger compile error

It is a "feature" of \csname that previously undefined commands are defined as if \let to \relax if accessed via \csname ... \endcsname. Usually this is unwanted behaviour but it is used in a few places in LaTeX as it is the only way in classic TeX to make a definition in an expansion-only context and so can be used to flag certain things. (It is used for example to trap problematic characters that are encountered during writing a file in filecontents.)


The solution is

\newcommand*\foo[1]{%
  \ifcsname foo@#1\endcsname
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
  {\csname foo@#1\endcsname}%
  {\ERROR}%
}

or, using a kernel construction

\newcommand*{\foo}[1]{%
  \@ifundefined{foo@#1}{\ERROR}{\csname foo@#1\endcsname}%
}