2

See the code below, the first two examples are fine while the last gave errors.

The possible reason is that \seq_if_in:NnTF is not expandable.

But I'd like to check the argument and print some error message if the argument is not contained in some sequence, and in the same time, make the command able to be used in \int_eval:n or nested.

Is there some way?

\documentclass{article}
\usepackage{xparse}

\begin{document} \ExplSyntaxOn \seq_set_from_clist:Nn \l_tmpa_seq { 1, 2, 3 } \cs_new:Nn __add_one_or_two:n { \seq_if_in:NnTF \l_tmpa_seq { #1 } { \int_eval:n { #1 + 1} } { \int_eval:n { #1 + 2} } } % example 1, fine __add_one_or_two:n { 3 } % example 2, fine __add_one_or_two:n { 4 } % example 3 \int_eval:n { 2 + __add_one_or_two:n { 5 } } \ExplSyntaxOff \end{document}

ZhiyuanLck
  • 4,516

1 Answers1

4

\seq_if_in:Nn(TF) is not expandable because the tokens in the sequence might have different catcodes:

\exp_args:NNx \seq_set_from_clist:Nn \l_tmpa_seq { \token_to_str:N : }
\seq_show:N \l_tmpa_seq
\seq_if_in:NnTF \l_tmpa_seq { : }
  { \show\T } { \show\F }

and you can't (reasonably) test that expandably.

Property lists, on the other hand, have the key always made into a string, so \prop_if_in:Nn(TF) can be expandable. If you can change the data type, you can abuse a property list as a catcode-less sequence that allows expandable querying:

\documentclass{article}
\usepackage{xparse}

\begin{document} \ExplSyntaxOn \prop_set_from_keyval:Nn \l_tmpa_prop { % v empty values 1 = , 2 = , 3 = } \cs_new:Nn __add_one_or_two:n { \prop_if_in:NnTF \l_tmpa_prop { #1 } { \int_eval:n { #1 + 1} } { \int_eval:n { #1 + 2} } } % example 1, fine __add_one_or_two:n { 3 } % example 2, fine __add_one_or_two:n { 4 } % example 3 \int_eval:n { 2 + __add_one_or_two:n { 5 } } \ExplSyntaxOff \end{document}