Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: rclass AND sclass AND eclass


From   Maarten Buis <[email protected]>
To   [email protected]
Subject   Re: st: rclass AND sclass AND eclass
Date   Wed, 30 Nov 2011 09:36:00 +0100

On Wed, Nov 30, 2011 at 1:25 AM, Sergiy Radyakin wrote:
> the documentation for Stata declares that each program can be either
> rclass, or sclass, or eclass (or nclass). See -help program-.
>
> In many cases this is indeed true, but as the programs become ever
> more complicated, this strict correspondence breaks down at
> some level. Here is a trivial demonstration:
>
> program foobar
>    foo
>    bar
> end
>
> program foo, rclass
>  return local result = 1
> end
>
> program bar, sclass
>  sreturn local result = 2
> end
>
> Here -foobar- is declared as nclass, and its subroutines are declared
> as rclass and sclass. A casual observer might inspect only the
> -foobar- main routine, and come to the conclusion that the r- and
> s-results are safe and not damaged by the command. While in fact
> the program makes use of both r-results and s-results, and hence this
> must be clearly documented.

I see your point, but it has never bothered me nor does it bother me
now. I guess I am just used to the fact that especially r-class
results won't survive any subsequent commands, and as a consequence I
have never been bitten by this. The trick is not to try to be smart,
and just store the results you need later on, in a scalar, matrix, or
macro as soon as it is created rather than rely on them surviving any
subsequent commands. I find that this creates easier to understand and
debug code, as this way the content of the scalar, matrix or macro is
explicitly assigned at the point it was created. Than again, I
probably find that easier/clearer because I am used to it.

One way to achieve what you want is to make the returned results local
rather than global. However, I guess that will break too many existing
programs to be feasible now.

-- Maarten

--------------------------
Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
Germany


http://www.maartenbuis.nl
--------------------------
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index