# [dev-context] A few questions

Thu Aug 13 21:01:52 CEST 2009

Am 07.08.2009 um 14:33 schrieb Aditya Mahajan:

> When I started using context, I was surprised that context did not
> have anything for optional arguments. We could have something like
>
> \definesinglearugment\foo[#1]#2{...}
>
> to be equal to
>
> \def\foo{\dosingleargument\dofoo}
> \def\dofoo[#1]#2{...}
>
> and
>
> \definesingleempty\foo[#1]#2{...]
>
> to be equal to
>
> \def\foo{\dosingleempty\dofoo}
> \def\dofoo[#1]#2{...}
>
> and similar things for \definedoublearugment, \definetripleargument,
> etc. I am not convinced that this is a good thing, but using
> something like this in the core may result in a 10% reduction in the
> code size and might kill a few dodos :)

It's now your turn to extend the macro to more optional arguments.

\def\define
{\doifnextoptionalelse\dodefine\nodefine}

\def\nodefine{\dodefine[0]}

\def\dodefine[#1]#2%
{\doifnextoptionalelse{\dododefine[#1]#2}{\nododefine[#1]#2}}

\def\dododefine[#1]#2[#3]#4%
{\definecomplexorsimple#2
\setvalue{simple\strippedcsname#2}{\getvalue{complex
\strippedcsname#2}[#3]}%
\ifcase0#1\relax
\setvalue{complex\strippedcsname#2}{#4}%
\setvalue{simple\strippedcsname #2}{#4}%
\or\setvalue{complex\strippedcsname#2}[##1]{#4}%
\or\setvalue{complex\strippedcsname#2}[##1]##2{#4}%
\or\setvalue{complex\strippedcsname#2}[##1]##2##3{#4}%
\or\setvalue{complex\strippedcsname#2}[##1]##2##3##4{#4}%
\or\setvalue{complex\strippedcsname#2}[##1]##2##3##4##5{#4}%
\or\setvalue{complex\strippedcsname#2}[##1]##2##3##4##5##6{#4}%
\or\setvalue{complex\strippedcsname#2}[##1]##2##3##4##5##6##6{#4}%
\or\setvalue{complex\strippedcsname#2}[##1]##2##3##4##5##6##7##8{#4}%
\or\setvalue{complex\strippedcsname#2}
[##1]##2##3##4##5##6##7##8##9{#4}%
\fi}

\def\nododefine[#1]#2#3%
{\ifx#2\undefined
\else
\showmessage{systems}{4}{\string#2}%
\fi
\ifcase0#1\def#2{#3}%
\or\def#2##1{#3}%
\or\def#2##1##2{#3}%
\or\def#2##1##2##3{#3}%
\or\def#2##1##2##3##4{#3}%
\or\def#2##1##2##3##4##5{#3}%
\or\def#2##1##2##3##4##5##6{#3}%
\or\def#2##1##2##3##4##5##6##7{#3}%
\or\def#2##1##2##3##4##5##6##7##8{#3}%
\or\def#2##1##2##3##4##5##6##7##8##9{#3}%
\else\def#2{#3}%
\fi}

\starttext

\define\foo{bar}

\define[2]\bar{bar:#1:#2}

\define[2]\help[me]{help #1 #2}

\foo

\bar{a}{b}

\help{john}

\help[you]{mike}

\stoptext

Wolfgang