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: How to define an external Mata class within the namespace of an ado-file


From   "Joseph Coveney" <[email protected]>
To   <[email protected]>
Subject   st: How to define an external Mata class within the namespace of an ado-file
Date   Mon, 13 Dec 2010 13:17:43 +0900

I'm writing a program that makes use of a parent (base) Mata class for
generalized linear model likelihoods.  The program, an ado-file, provides one
subclass as a default (binomial distribution family and logit link function).
But, I would like for users to be able to extend the program with likelihoods of
their own, without having to modify the original ado-file.  The idea is for
users to create their own likelihood subclasses in external Mata code that they
write as needed.  The ado-file will, at the users' option, call this external
Mata code, instantiate its contained subclass, and use that object in lieu of
the default logistic object.

The problem is that the ado-file's Mata code is run within the namespace of the
ado-file:  the parent/base class is not visible to the external Mata code, and
so the external Mata file cannot extend it.

My workaround so far is to use -include- within the body of the ado-file's ado
program code, as shown below (it's simplified).  This seems to work, but the
help file for -include- admonishes against this: see -help include##remarks3- .


Is there a better way?

Joseph Coveney

*! gjps.ado
program define gjps, eclass
   version 11.1
   syntax /* . . . */, [EXTernal(string)]

   if "`external'" != "" include `"`external'"'

   mata:Controller::fit()
end

version 11.1
mata:
mata set matastrict on

class Likelihood {
   /* interface defined with virtual functions */
}

class Logistic extends Likelihood {
   /* . . . */
}

class Model {
   /* . . . */
   public:
        void setLikelihood()
        /* . . . */
}
void function Model::setLikelihood(
   pointer(class Likelihood scalar) scalar likelihood) {
   /* . . . */
}

class Controller {
/* Among other things, passes pointer to instance 
   of Logistic or externally defined likelihood class
   to instance of Model. */
   public:
        static void fit()
   /* . . . */
}
void function Controller::fit() {
   /* . . . */
}

end

*** End gjps.ado -- begin external file ***

<mylikelihood.mata>

version 11.1
mata:
mata set matastrict on

class MyLikelihood extends Likelihood {
   /* . . . */
}
/* . . . */

end
exit


*
*   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