Announcement

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

  • Understanding Stata's Comment Hierarchy

    I was hoping to get feedback in understanding Stata comments. The documentation was helpful but I wonder if I'm missing anything:

    - Multi-line comments can be nested. This means that every "/*" within a multi-line comment must be closed.

    - Stata ignores everything within "/*" and "*/" across lines unless the first "/*" appears in a line that starts with "//" or after " //".

    - Stata ignores everything after in-line comments: "//" at the start of a line or after " //" at the end of a line (given they is not preceded by "/*").

    - Stata ignores code after "*" at the start of a line or "*" directly after a multi-line comment. However, line continuations (" ///"), multi-line comments ("/*" to "*/"), and in-line comment rules (" //") apply after a "*".

    If you run the following:

    Code:
    * /* This will be a multi-line comment
    disp "Not printed"
    */
    
    * // /* Ignored due to inline comment
    disp "Printed 1"
    
    // /* Also ignored due to inline comment
    disp "Printed 2"
    
    *// /* This is not an inline comment, so this is multi-line again
    disp "Not printed"
    */
    
    * ///
    disp "Not printed. Line continuation applies"
    
    // /// Line continuation ignored due to inline comment
    disp "Printed 3"
    
    /*
    /* Nested */
    disp "Not printed"
    */* disp "Not printed"
    (Note that if "*/*" was replaced with "*///" we would get an error). If I am missing something in terms of comment hierarchy, I would be really thankful for input. Cheers!

  • #2
    Mauricio, thing get even more fun once you bring in the delimiter ;

    See this thread:
    https://www.stata.com/statalist/arch.../msg00040.html

    Best, Sergiy Radyakin

    Comment


    • #3
      Indeed. "#delim ;" makes this quite interesting. But I think I figured it out:

      1. "//" means ignore everything until the next line break (carriage return).

      2. "///" means ignore everything until the next carriage return, including the line break (carriage return).

      3. "*" means ignore all code (but, crucially, not all comments) until the next delimiter, which is carriage return or ";"

      Code:
      #delimit ;
      
      // This line is ignored, but the line break is not
      di "Printed 1";
      
      // Same for multi-line /*
      di "Printed 2";
      
      * Line continuations do apply
      di "Not printed";
      
      * Same for multi-line /*
      di "Hello one"  */;
      Moreover:

      Code:
      #delimit cr
      
      * Line continuation ///
      // Breaks line continuation ///
      di "Printed 1"
      
      disp "Line continuation" ///
      // Breaks line continuation ///
      di "Printed 2"
      
      #delimit ;
      
      * Line continuation
      // Does not break line continuation
      di "Not printed";
      
      disp "Line start"  
       // This is ignored, and does not give error
      "; Line end" ;
      
      disp "Line start"  
      // This does give an error
      "; Line end" ;
      The last one is annoying because there is a trailing space, but it seems Stata strips trailing spaces when joining lines. These also work:
      Code:
      local ninety 90
      disp `ni/*9*/nety'
      disp `ni/*
      
      */nety'
      disp `ni ///
      nety'
      The last one displays nothing, however, since it's effectively calling `ni nety'.

      Comment


      • #4
        FYI, this is now fixed in the syntax highlighter for Atom (except for the corner cases with #delimit ; )

        Click image for larger version

Name:	Screen Shot 2018-06-11 at 12.35.57 PM.png
Views:	1
Size:	190.9 KB
ID:	1448476

        Comment

        Working...
        X