Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • use of -st_global()-

    Hi -- I am trying to understand the purpose of the mata function -st_global()-. Because it just returns the contents in string form of what is in the named global macro, I do not understand why it is needed. In particular, if we have some macro with name -macName-, can't we just get the same result with "$macName" ? Here is an example. Comment? -- P

    . // looking at retrieving the contents of a global macro in mata
    . global testmac wow. what a fun narrative

    . dis "$testmac"
    wow. what a fun narrative

    . mata
    ------------------------------------------------- mata (type end to exit) -----
    : "$testmac"
    wow. what a fun narrative

    : maccontents=st_global("testmac")

    : maccontents
    wow. what a fun narrative

    :
    : end
    -------------------------------------------------------------------------------


  • #2
    The core idea is that Mata is a compiled language. That means you typically do not use it interactively but write functions that you call later. When writing a function as

    Code:
    void printf_testmac()
    {
        printf("${testmac}\n")
    }
    there are two major problems. First, you probably do not want to hardwire the name testmac into that function. But, say, you did, there is another problem. By the time you compile your function (compile time), it substitutes ${testmac} for whatever the contents of global testmac is then. And, when you call your function later, it will re-display this content, regardless of the contents of global testmac at run time. Here is what that looks like:

    Code:
    . dis "$testmac"
    wow. what a fun narrative
    
    .
    . mata :
    ------------------------------------------------- mata (type end to exit) -----------------------------------------------------------------------------------------------------------------------------
    :
    : void printf_testmac()
    > {
    >     printf("${testmac}\n")
    > }
    
    :
    : end
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    .
    . mata : printf_testmac()
    wow. what a fun narrative
    
    .
    . global testmac "This will not show in Mata"
    
    . display "$testmac"
    This will not show in Mata
    
    .
    . mata : printf_testmac()
    wow. what a fun narrative
    This is almost certainly not what you want.

    Here is the same thing - hardwired name - but using st_global():

    Code:
    . mata :
    ------------------------------------------------- mata (type end to exit) -----------------------------------------------------------------------------------------------------------------------------
    :
    : void printf_testmac()
    > {
    >     printf("%s\n", st_global("testmac"))
    > }
    
    :
    : end
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    .
    . mata : printf_testmac()
    wow. what a fun narrative
    
    .
    . global testmac "This will show in Mata"
    
    . display "$testmac"
    This will show in Mata
    
    .
    . mata : printf_testmac()
    This will show in Mata
    Last edited by daniel klein; 19 Feb 2024, 14:46.

    Comment


    • #3
      st_global() is used to get and set contents of return, ereturn and creturl macros directly. You cannot access r() and e() without it.

      Comment


      • #4
        Daniel -- This is an awesome answer! I think I learned my lesson! I have to go back and look carefully at my use of macros in my mata code! -- P

        Comment


        • #5
          Hi Again -- I am *pretty sure* this is not a related issue, but you now have me thinking about these things. In the attached screenshot from p.307 of the [BAYES] manual, I see that in a -stata- program (not a -mata- function), the authors define local macros `d' and `n' from global macros $MH_yn and $MH_n (in red). Then, they pass the contents of `d' and 'n' to a -mata- function. BUT, is there. any reason to define `d' and `n' here? Could they not just have passed the contents of $MH_yn and $MH_n to the -mata- function?

          Screenshot 2024-02-19 at 6.33.10 PM.png

          Comment


          • #6
            Technically, there is no apparent reason to define these local macros. Perhaps, the authors intended to make the code more readable, which is always a good idea (that I do not follow nearly as often as I should).

            Comment

            Working...
            X