## LATEX-L@LISTSERV.UNI-HEIDELBERG.DE

 Options: Use Forum View Use Monospaced Font Show HTML Part by Default Show All Mail Headers Message: [<< First] [< Prev] [Next >] [Last >>] Topic: [<< First] [< Prev] [Next >] [Last >>] Author: [<< First] [< Prev] [Next >] [Last >>]

 Subject: Re: expl3 global vs local assignments From: Joseph Wright <[log in to unmask]> Reply To: Mailing list for the LaTeX3 project <[log in to unmask]> Date: Mon, 15 Mar 2010 10:51:32 +0000 Content-Type: text/plain Parts/Attachments: text/plain (78 lines)
On 14/03/2010 23:22, Will Robertson wrote:
> On 14/03/2010, at 11:36 PM, Will Robertson wrote:
>
>> The code is somewhat tentative but I think it shows this approach is valid. Any comments or suggestions?
>
> Well, I do notice that etex.sty does a lot of this stuff as well, so I really should be working off that package rather than re-inventing the relevant parts of it... in fact, it almost replaces entirely the need for the expl3 stuff; just need to say (I think)
>
> \let \int_new:N       \globcount
> \let \int_new_local:N \loccount
>
> etc. etc.
>
> Should have looked in there first :) I didn't realise it provided support for local registers already.

So the question "is this okay on LaTeX2e" is not a problem (as expl3
loads etex anyway). One thing I'd be tempted to do would be:

\int_new:N  \l_scratch_int % Local
\int_gnew:N \l_some_int    % Global

where \int_new:N checks \currentgrouplevel, and allocates from the
global pool (low to high numbers) if it's used outside of any group, and
from the local pool (high to low numbers) otherwise. (This is pretty
easy to do, and I'd hope is not too "costly".)

My reasoning for this is that we have set/gset, and so are (in the main)
favouring local unless specified.

---

I had a look at what the original example was for local declarations.
The lead off was the C++ model:

for (...) {
int j = ...
}

which is considered better than:

int j;
for (...) {
j = ...
}

(I know very little C++, so I'm going to assume that this is correct!)

The LaTeX2e analogy was then:

\whiledo{...}{
\newcount\j
}

But the allocations here are all at the same group level, so I'm not
sure that the example quite works. I guess better is

\cs_set:Npn \some_function:<args> #1... {
\group_begin:
\int_new:N \l_some_int % Assuming my new/gnew approach => local.
% Do stuff
\group_end:
}

Now that's okay provided there is no nesting of \some_function:<args>,
as local in a TeX sense is not the same as local in an OO sense.
Assignments aren't expandable and so we are safe from some kind of
recursive, expansion-based nesting. However, are we safe from all
nesting? I can't currently think of a realistic example where we might
run into trouble, but perhaps I'm lacking imagination.

---

My conclusion from the above is I'm quite happy to go for local
assignments and a C++-like model for where variables should be created,
unless there is a good reason to think that some kind of nested _new
situation is likely to occur. Even then, I guess you can just warn about it.
--
Joseph Wright