Announcement

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

  • Testing class functions interactively

    I am creating a class in Mata and want to test the class functions interactively in Stata. How do I go about doing this? I tried looking for this in the Mata Book and was unable to find any guidance on this.

  • #2
    Originally posted by Ian Xu View Post
    I am creating a class in Mata and want to test the class functions interactively in Stata. How do I go about doing this?
    After you've created the class definition, open an interactive session of Mata, and instantiate an object of the class. Then you can call public methods on it.

    I illustrate below.
    Code:
    version 19
    
    clear *
    
    mata:
    mata set matastrict on
    
    class Probe {
        private:
            string scalar msg
        public:
            void setMe(), report()
    }
    void function Probe::setMe(string scalar msg) {
        this.msg = msg
        printf("All set!\n")
    }
    void function Probe::report() printf("I've been set as: %10s\n", msg)
    
    end
    
    * In an interactive session, instantiate an object of the class
    
    mata
    
    p = Probe() // <= Here
    
    // Then you can test public methods like this:
    
    p.setMe("interactive test")
    
    p.report()
    
    end
    
    exit

    Comment

    Working...
    X