The laws of expansion (\chardef\foo=<number>\foo)
The key is that \chardef\<name>
turns \<name>
temporarily into \relax
. We can see this with \tracingall
:
\def\foo{1}
\tracingall
\chardef\bar=3\foo\bar
yields
{\chardef}
{changing \bar=macro:->\mathaccent "7016\relax }
{into \bar=\relax}
\foo ->1
{changing \bar=\relax}
{into \bar=\char"1F}
{\char"1F}
whereas with
\def\foo{1}
\tracingall
\chardef\foo=3\foo
we get
{\chardef}
{changing \foo=macro:->1}
{into \foo=\relax}
{changing \foo=\relax}
{into \foo=\char"3}
{\char"3}
So we get the outcomes you observe, not because of \chardef
failing to expand material but because of the nature of the material at the point it is examined.
Knuth says:
\TeX\ takes precautions so that constructions like `|\chardef\cs=10\cs|' and
`|\font\cs=name\cs|' won't expand the second |\cs| until
the assignments are done.
(Chapter 24, 'Summary of Vertical Mode, page 278).
Quoting the TeXbook (p. 278)
TeX takes precautions so that constructions like ‘
\chardef\cs=10\cs|
’ and ‘\font\cs=name\cs
’ won’t expand the second\cs
until the assignments are done.
Those precautions are covered in module 1224 of tex.web
:
A ‘shorthand def’ is one of the definition-like primitives described in the module.
Knuth warns that the book doesn't always tell the complete truth.