Announcement

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

  • Do "//" and "*" work differently in Mata?

    I just found that // and * work differently in Mata.
    // works as expected, but * works inconsistently.

    The following example shows that both // and * work as commenting commands.
    Code:
    . clear all
    . mata
    :         h=10
    :         h
      10
    : end
    
    . 
    . clear all
    . mata
    :         //h=10
    :         h
                     <istmt>:  3499  h not found
    r(3499);
    : end
    
    . 
    . clear all
    
    . mata
    :         *h=10
                     <istmt>:  3499  h not found
    r(3499);
    :         h
                     <istmt>:  3499  h not found
    r(3499);
    : end
    However, the following example shows that * does not work as a commenting command.
    Code:
    . clear all
    . mata
    :         h=10
    :         st_numscalar("H",h)
    : end
    . di H
    10
    
    . 
    . clear all
    . mata
    :         h=10
    :         //st_numscalar("H",h)
    : end
    . di H
    H not found
    r(111);
    
    
    . clear all
    . mata
    :         h=10
    :         *st_numscalar("H",h)
                     <istmt>:  3257  nonpointer found where pointer required
    r(3257);
    : end
    . di H
    10

  • #2
    See -help comment-:

    Code:
        The comment indicator * may be used only at the beginning of a line, but
        it does have the advantage that it can be used interactively.  * indicates
        that the line is to be ignored.  The * comment indicator may not be used
        within Mata.
    Also -help m2_comments- (esp. bolded portions):

    Code:
    Syntax
    
            /* enclosed comment */
    
            // rest-of-line comment
    
    
        Notes:
    
            1.  Comments may appear in do-files and ado-files; they are not allowed interactively.
    
            2.  Stata's beginning-of-the-line asterisk comment is not allowed in Mata:
    
                    . * valid in Stata but not in Mata
    Last edited by Ali Atia; 15 Jul 2021, 13:08.

    Comment


    • #3
      Cross-posted at https://stackoverflow.com/questions/...rently-in-mata

      Please note our policy on cross-posting, which is that you should tell us about it. https://www.statalist.org/forums/help#crossposting

      Comment


      • #4
        Mata has been consistent in your two examples: In both cases using the * as a comment did not work. Notice that in your first example, where you claimed it did work, you did get an error message immediately after you used *h. This makes sense if you know what *h means in Mata. The * is used in Mata to either multiply (like in Stata) or to get at the content of something called a pointer. So if you type *h in Mata, like you did in your example, then Mata thinks that there is a pointer called h and you want to get access to whatever h points to. Since you did not have a pointer called h, you got the error message "h not found". This also explains the error message in your second example: st_numscalar() is a function and not a pointer and trying to use a pointer operator on a non-pointer makes no sense, and that is exactly what the error message tells you.

        If you don't know what pointers are, then you probably don't need to know. In that case all you need to remember is to not use a * for a comment in Mata.
        ---------------------------------
        Maarten L. Buis
        University of Konstanz
        Department of history and sociology
        box 40
        78457 Konstanz
        Germany
        http://www.maartenbuis.nl
        ---------------------------------

        Comment

        Working...
        X