How can I stop function from running rest of the code if some conditions are met in latex3?
Now I have to build nested conditions and have a couple of default cases duplicated. Code readability gets worse if the default case is quite huge.
\ExplSyntaxOn
\cs_set:Npn \my_func #1 {
\token_if_cs:NTF #1 {
\str_eq:nnTF {#1} {\\} {
first case
}{
\token_if_expandable:NTF #1 {
second case
}{
default
}
}
}{
default
}
}
\my_func{\\}
\ExplSyntaxOff
Here's what I'd like to have in terms of programming language
function(arg){
if(condition 1){
return <first case>;
}
if(condition 2){
return <second case>;
}
return <default case>;
}
Best Answer
There are a few ways to achieve this. I'd likely use a predicate-based approach and lazy evaluation:
For more complex cases, I would usually put the 'payload' (actions) into auxiliaries.
If you are wedded to the 'return' format, you need some end marker token
but honestly I would stick to predicates and appropriate auxiliaries.