texit-conditions.tex /size: 2785 b    last modification: 2020-07-01 14:35
1\environment texit-style
2
3\startcomponent texit-conditions
4
5\startchapter[title={Conditions}]
6
7In case you wonder why we have modes in \CONTEXT, here is an example that might
8convince you. The \TEX\ language has conditionals and they are in fact quite
9efficient, take for instance:
10
11\startTEX
12\ifnum\scratchcounter>10
13    \ifdim\scratchdimen>10pt
14        one
15    \else
16        two
17    \fi
18\else
19    three
20\fi
21\stopTEX
22
23When the first test fails, \TEX\ will do a fast scan over the following tokens
24and expand the \type {three} branch. In order to do such a fast scan, the nested
25condition needs to be properly balanced: the \type {\else} is optional but the
26nested \type {\fi} definitely isn't. Now imagine that you use a few pseudo
27booleans, like:
28
29\startTEX
30\newif\ifalpha \alphatrue
31\newif\ifbeta  \betatrue
32\stopTEX
33
34And you need it in:
35
36\startTEX
37\ifalpha
38    \ifbeta
39        YES
40    \else
41        NOP
42    \fi
43\else
44    NOP
45\fi
46\stopTEX
47
48This happens occasionally in real applications and one can either repeat the
49\type {NOP} or wrap it in a macro in order to save tokens. However, way more
50natural would be something like this:
51
52\startTEX
53\ifalphaorbeta
54    YES
55\else
56    NOP
57\fi
58\stopTEX
59
60This basically would introduce a new kind concept: an expandable macro flagged as
61\type {\if} kind of token. I actually experimented with that in \LUATEX\ but
62rejected it eventually. Instead \type {\ifcondition} was introduced. This is
63basically equivalent to \type {\iffalse} when \TEX\ is in fast \type {\if*}
64skipping mode, but when a real test happens the next argument is expanded. That
65macro is expected to end up as something equivalent to \type {\iftrue} or \type
66{\iffalse} so that other the nexct branch or the \type {\else} is entered. Here
67is an example:
68
69\startTEX
70\ifcondition\alphaorbeta
71    YES
72\else
73    NOP
74\fi
75\stopTEX
76
77There are several ways to define \type {\alphaorbeta} now and we show a few here.
78It's up to you to figure out which ons is the most efficient.
79
80\startTEX
81\def\alphaorbeta{\ifcase0\ifalpha \else\ifbeta \else1\fi\fi\relax}
82\def\alphaorbeta{\ifcase \ifalpha0\else\ifbeta0\else1\fi\fi\relax}
83\def\alphaorbeta{\ifnum1=\ifalpha1\else\ifbeta1\else0\fi\fi\relax}
84\def\alphaorbeta{\ifnum 0\ifalpha1\fi  \ifbeta1\fi       >1\relax}
85\stopTEX
86
87Now, do we expect users to come up with such constructs? Of course not. Even in
88\CONTEXT\ we don't really need them, although there are a few places where they
89can be used. In \CONTEXT\ you would just do this:
90
91\startTEX
92\enablemode[alpha]
93\enablemode[beta]
94
95\doifelsemode {alpha,beta} {
96    YES
97} {
98    NOP
99}
100\stopTEX
101
102Of course such a verbose macro is less efficient but even if you use this test
10310.000 times in a run it will not take more than 0.06 seconds on a decent 2013
104laptop.
105
106\stopchapter
107
108\stopcomponent
109