Hello,
I have a need to pass an instance of a class created in Mata to Stata and then get it back from Stata. I tried different approaches but each was blocked by one Mata limitation or another:
1. The most straightforward solution is to return a transmorphic scalar/matrix (or a pointer to it) to Stata. But st_numscalar, st_local etc only accept a real or string parameter, and there is no way that I could find to convert between transmorphic scalar/matrix (or a pointer to it) and real or string scalar.
2. Another solution is to hold the instances in an asarray/vector in Mata and pass to Stata a handle (real scalar) that when passed back by Stata can be used to retrieve the corresponding instance from the asarray/vector. The obstacle here is that the asarray/vector must persist between function calls, and I could not find a way to create a global Mata variable (AFAIK, Mata does not support declaring variables outside functions) or the equivalent of C static functions.
3. Also tried to create a class with a public static transmorphic scalar as a member and a static accessor function, but any assignment to the static member in the static function results in error 3000 invalid lval.
Does anyone know a way around or possibly a totally different solution.
Thanks,
I have a need to pass an instance of a class created in Mata to Stata and then get it back from Stata. I tried different approaches but each was blocked by one Mata limitation or another:
1. The most straightforward solution is to return a transmorphic scalar/matrix (or a pointer to it) to Stata. But st_numscalar, st_local etc only accept a real or string parameter, and there is no way that I could find to convert between transmorphic scalar/matrix (or a pointer to it) and real or string scalar.
2. Another solution is to hold the instances in an asarray/vector in Mata and pass to Stata a handle (real scalar) that when passed back by Stata can be used to retrieve the corresponding instance from the asarray/vector. The obstacle here is that the asarray/vector must persist between function calls, and I could not find a way to create a global Mata variable (AFAIK, Mata does not support declaring variables outside functions) or the equivalent of C static functions.
3. Also tried to create a class with a public static transmorphic scalar as a member and a static accessor function, but any assignment to the static member in the static function results in error 3000 invalid lval.
Does anyone know a way around or possibly a totally different solution.
Thanks,
Comment