0

Have been reading the expl3 document interfaces3.pdf to understand how to declare and use functions. Although I have understood that there are three main methods (new, set, gset) where each method has specific use cases and restrictions upon parameter arguments.

Have also covered quite well the protected from expansion variants in certain contexts.

I would like some explanation about the difference between defining new functions using parameter text and defining new functions using the signature as covered in sections 4.3.2 and 4.3.3.

cfr
  • 198,882
Veak
  • 1
  • 4
    you are presumably refering to \cs_new:Nn verses \cs_new:Npn which is covered in interface3.pdf but you haven't given any example or mentioned expl3, so that's just a guess. – David Carlisle Sep 29 '23 at 22:40
  • set/gset are not exactly different ways of defining from new. They're intended for redefining functions already declared with new. So you shouldn't do set or gset unless you did new or you're explicitly wanting to redefine a function declared with new elsewhere. – cfr Sep 30 '23 at 01:29
  • @cfr That is ok. The biggest wonder is parameter text versus signature. – Veak Sep 30 '23 at 01:34
  • It's not versus signature. All expl3 functions have to have a signature. It's whether that signature is used to determine the expected parameters or whether the parameters are specified separately. – cfr Sep 30 '23 at 01:55
  • I don't know if my answer answers your question. The idea of parameters here is a TeX one. But I thought an example of why you might want the parametised form might help, since otherwise it might seem superfluous. Some people seem to use it as their default choice, whereas I'm lazy and let LaTeX figure it out from the signature when I can. – cfr Sep 30 '23 at 01:59

1 Answers1

1

The code examples below are either from Joseph Wright or derived from code from Joseph Wright in answer to my (much) earlier question.

In standard cases, the difference is just one of how you prefer to specify the function. In the parametised case you specify the function's parameters explicitly:

\cs_new_protected_nopar:Npn \__chronos_set_minoryearformat:n #1
{
  \tl_set:Nn \l__chronos_minoryearformat_tl { #1 }
  \tl_replace_all:Nnn \l__chronos_minoryearformat_tl { ~ } { \c_space_token }
}

That is, the #1 tells LateX the function takes a single argument, just as \def\formatyear#1{ ... } does.

In the non-parametised case, LaTeX infers the parameters from the new function's signature:

\cs_new_protected_nopar:Nn \__chronos_set_minoryearformat:n 
{
  \tl_set:Nn \l__chronos_minoryearformat_tl { #1 }
  \tl_replace_all:Nnn \l__chronos_minoryearformat_tl { ~ } { \c_space_token }
}

That is the n tells LaTeX the function takes a single argument, just as the 1 does in \newcommand*\formatyear[1]{ ... } or m does in \NewDocumentCommand\formatyear{m}{ ... }.

But in some cases, you want the parametised version because you want to handle the arguments passed to the function specially.

\cs_new_protected_nopar:Npn \__chronos_set_date_aux_ce:w #1 - #2 - #3 - #4 @#5 \q_stop
{
    \__chronos_set_date:nnnn {#1} {#2} {#3} {#5}
}

So this function doesn't expect some mixture of tokens and braced lists of tokens. It expects something with a non-standard syntactical form.

This makes it possible to parse user input of dates which differ in how much information they provide. Basically, the user can say

date={1066-08-09}

or

date=1066

or

date={1066-03}

The package then adds to the user-specified input

<user-specified date>-01-01-0@start \q_stop

and the code will pass the appropriate information on. If the user only specified a year, a month and day will be added. If the user specified only a year and month, a day will be added. If the user specified a full date, nothing will be added except start which is used to create variable names.

This is why w. w is for 'weird'.

This is basically like similar definitions at the TeX level.

\def\mydateparser@aux#1-#2-#3-#4@#5\@nil{ ... }

would work similarly (except this isn't protected and there is no check to ensure the definition isn't obliterating some crucial thing).

cfr
  • 198,882
  • Right, I have only had experience with non-parametised version, where I do not specify #1 at the end. – Veak Sep 30 '23 at 02:45
  • How would you describe the non-parametised and parametised versions briefly. Haw would you describe them ? – Veak Sep 30 '23 at 03:02
  • This does not say much in terms of understanding p and w These are special cases. – Veak Sep 30 '23 at 03:03
  • @Fluffy See edit. – cfr Sep 30 '23 at 03:30
  • 2
    @Fluffy the p argument is a special case and means parameter text. – Skillmon Sep 30 '23 at 09:34
  • @cfr, May I ask where you got the example code from? I do not see a package called "chronos" on CTAN. (although chronosys is available) Also I do not see anything about "chronos" on Joseph Wright's github page. – User23456234 Oct 01 '23 at 04:30
  • 1
    @User23456234 https://tex.stackexchange.com/q/327619/ is the question. https://tex.stackexchange.com/a/327642/ is Joseph's answer, referenced above. I've edited the links into the answer. – cfr Oct 01 '23 at 04:50