I've used the \relax side-effect of \csname ...\endcsname for three purposes.
The first
is similar to how LaTeX uses it in \end{...}. Let's call TeXu a variant of TeX in which \csname ...\endcsname leaves undefined control sequences undefined.
We have a list of keys, and associated actions (arbitrary TeX code), and we want to define \act{#1} to do the action labelled by the key #1. First define the various actions.
\def\actA{\message{I found A.}}
\def\actB{\message{Oh, here is a B!}}
\def\actKEY{\message{The key was KEY.}}
If you want \act to produce no error on undefined keys, you would do
% in TeX
\def\act#1{\csname act#1\endcsname}
% in TeXu
\def\act#1%
{%
\expandafter\ifx\csname act#1\endcsname\undefined
\else
\csname act#1\expandafter\endcsname
\fi
}
Now, you could argue that having an error would be better, since we'd want to catch typos in key names. So how do TeX and TeXu compare in that case? Well, \def\act#1{\csname #1\endcsname} would work in TeXu. However, is Undefined control sequence \abcKEYZ really the best error message? For a better error message, we can do
% in TeX
\def\act#1%
{%
\expandafter\ifx\csname act#1\endcsname\relax
\errmessage{Unknwon key #1}%
\else
\csname act#1\expandafter\endcsname
\fi
}
% in TeXu, same, with `\relax` replaced by `\undefined`.
However, with TeX we can do something slightly faster:
% in TeX
\def\actA#1#2{\message{I found A.}}
\def\actB#1#2{\message{Oh, here is a B!}}
\def\actKEY#1#2{\message{The key was KEY.}}
\def\act#1%
{%
\csname act#1\endcsname
\errmessage{Unknown key #1}%
}
If the key is known, the \act... function removes the error message, and if it is unknown, the undefined \csname construction turns into \relax, and an error message is displayed. There is no equivalent for TeXu. At the time of writing, this construction is used in l3kernel for \prg_new_conditional:Npnn and related functions (where valid keys are the conditional forms T, F, TF, and p), and for \int_compare:nTF (where valid keys are the comparison operators <, =, >).
The second
is for reporting special conditionas in an expandable setting. I use this in the LaTeX3 floating point module l3fp (found in the l3kernel bundle), and in the string conversions in l3str (in the l3experimental bundle).
Let's look at l3fp. \fp_to_tl:n {#1} evaluates the floating point expression #1 and turns it into a list of tokens (such as 0.123 or -1.4e-789 or -inf), expandably. When plotting graphs, for instance, we want \fp_to_tl:n { 1/0 } to simply give inf, but in other contexts this should trigger an error. I've recently added an experimental feature (not fully implemented) where one can test if a division by zero or another special condition occurred during a computation. If we were doing computations expandably, it would be trivial to set a switch to true if there was an error. In an expandable setting, this is impossible, and the only thing we can do is use the \relax side-effect of \csname...\endcsname. Namely, when such a division by zero occurs in a computation, a specific control sequence is made into \relax, and the user can query that after the end of the computation (or, more deviously, within the computation itself...).
In l3str, I want to be able to convert potentially very long strings from one encoding to another. This cannot be done by converting characters and storing them one by one in a token list (parameterless macro): since each addition takes a time proportional to the number of characters converted so far, the total time becomes quadratic in the length of the string, typically too long. Instead, the string is converted in one go. For example, say that I wish to convert a string of characters to a list of their character codes (in decimal notation), separated by commas.
\def\to@charcodes#1%
{\expandafter\to@charcodes@\detokenize{#1}{\fi\iffalse\iffalse}\fi\fi}
\def\to@charcodes@#1%
{\iffalse#1\fi\number`#1,\to@charcodes@}
\message{\to@charcodes{ABC}}
does the trick. Now, most of the time in conversions, the input string may not be valid. Assume for our example that we want a warning if A appears in the string. Then we simply add \if#1A\flag@on\fi to \to@charcode, with \def\flag@on{\expandafter\iffalse\csname some@flag\endcsname\fi}. To get a warning:
\ifx\some@flag\relax
\message{Warning: backslash detected!}%
\fi
The third
is to produce pseudo-random numbers expandably without using engine-dependent primitives (see the pathetically slow l3rand in l3trial, found e.g., on the GitHub repo). The main difficulty is to have a command which expands differently each time it is called. This can be done with a combination of \csname...\endcsname and eTeX's \ifcsname. For instance, we can define a function which counts how many times it was called:
\def\howmanytimes{\howmanytimes@0;}
\def\howmanytimes@#1;%
{%
\ifcsname howmanytimes@#1\endcsname
\expandafter\howmanytimes@\number\numexpr 1+%
\else
\expandafter\howmanytimes@end
\fi
#1;%
}
\def\howmanytimes@end#1;%
{%
\expandafter\iffalse\csname howmanytimes@#1\endcsname\fi
#1%
}
\message{\howmanytimes}
\message{\howmanytimes}
\message{\howmanytimes}
\message{\howmanytimes}
prints 0 1 2 3.
\csname ...\endcsnamedid not leave things as\relax, what would the construct give? This is used for example by LaTeX's\end{...}, where you don't have to have a definition for the\end<foo>macro for an environment to work. – Joseph Wright Aug 10 '12 at 21:16\end{...}could be defined to first check if the\end<foo>macro exists before attempting to invoke it (and if not, then just do nothing), and so this isn't such a make-or-break issue. – cyberSingularity Aug 10 '12 at 22:46