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]

st: reciprocal references between classes in Mata


From   Phil Schumm <[email protected]>
To   Statalist Statalist <[email protected]>
Subject   st: reciprocal references between classes in Mata
Date   Fri, 6 Sep 2013 11:54:26 -0500

Hi,

At the risk of drawing scorn from the Mata cognescenti (I feel like I'm going to realize that the answer to this question is obvious as soon as I click "Send"), consider the following (simplified) example:


    class Results {
        real scalar                                 primary_outcome
        pointer(class Study scalar) scalar          study
        void                                        myfunc()
    }

    void Results::myfunc()
    {
        study->type
    }

    class Study {
        string scalar                               type
        class Results scalar                        results
        void                                        new()
    }

    void Study::new()
    {
        type = "RCT"
        results = Results()
        results.study = &this
    }


As you can see, I have a Study class, which has as an attribute an instance of a Results class.  In addition, there are behaviors I'd like to implement for the results that depend on features of the study (e.g., in this case, the study type).  The problem is, I can't figure out the best way (or even any way) to refer to the study from within the results instance.

Clearly what I've written above won't work, because the compiler won't recognize the Study class when compiling Results (and I can't switch the order of Results and Study, since that will cause the same problem in reverse).  I feel like I should be able to declare the Study class (i.e., like in C) so that the compiler knows a definition is coming, but I don't know how to do that in Mata.

Note that this issue also affects other possible strategies for referring to Study from within Results.  For example, if I were to store the Study instance in a global variable, I would still need to declare a local variable to hold it from within a method in Results, and this would then create the same problem when compiling.

Any suggestions would be most welcome.

Thanks,


-- Phil


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


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