I presume these problems are related.
\documentclass[a4paper]{article}
\begin{document}
\message{\edef\foo{Foo}\foo}
% gives
%! Undefined control sequence.
%l.5 \message{\edef\foo
% {Foo}\foo
\newcommand\testing[2][foo]{#1 = #2}
\testing[Foo]{Bar} % works
\testing{bar} % works
\directlua{\testing[Foo]{Bar}}
% or
\directlua{\testing{bar}}
% gives
%! Use of \\testing doesn't match its definition.
%\kernel@ifnextchar ...rved@d =#1\def \reserved@a {
% #2}\def \reserved@b {#3}\f...
%
%l.16 \directlua{\testing
% [Foo]{Bar}}
\end{document}
There are many other versions of these. \defs and \edefs are useful when writing macros. I don't know where optional arguments are officially documented (I only have the TeXbook and Lamport's book and this postdates these) but the instructions I read on-line for optional arguments doesn't say anything about the resulting macros only being available for use in certain sections of code.
Please could someone explain what's going on and tell me where I can find the full gory details: in particular what can and cannot be put inside \directlua and what other TeX/LaTeX commands behave in this way. I am writing quite a lot of macros to go inside \directlua, and don't want to be caught out again!
\defworks by expansion, it describes the expansion of the command defined by\def. – David Carlisle Feb 15 '20 at 23:04\newcommand\greeting[1]{Hello #1!}EXPANDS into\def\greeting#1{Hello #1!}(same link) is misleading and typical. You just told me that optional arguments are not implemented in LaTeX using pure expansion! But this is just quibbling - I learnt a lot, thank you! – user2444353 Feb 16 '20 at 00:35