Announcement

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

  • nothing found where '(' expected in class initialization

    Hi,

    I'm having trouble getting through this. I have the following class
    Code:
    version 15
    set matastrict on
    
    mata:
    class nelogit
    {
        protected:
            real matrix X, S                                                        // explanatory variables and scale variables
            real colvector y                                                        // vector of the dependent variable
            real scalar j                                                            // number of alternatives
        
        public:
            void setup()
    }
    
    // Setup functions for the class
    void nelogit::setup(real colvector user_y,
        real matrix user_X,
        real scalar user_j,
        | real matrix user_S)
    {
        y = user_y
        X = user_X
        j = user_j
        if (args()>3) S = user_S
    }
    
    end
    I then have a file test.do that does the following
    Code:
    version 15
    
    local pdir "/Users/alfonso/Dropbox/My Documents/Programming/Stata/ado/nelogit/Classes"
    cap mata: mata drop nelogit()
    quiet do "`pdir'/nelogit.mata"
    
    // Loading data set
    local ddir "/Users/alfonso/Dropbox/Academia/My Research/My Articles/Health Mixed Logit/Stata Files"
    use "`ddir'/Data/estimationdata", clear
    // Getting rid of invalid observations
    drop if missing(Answer, Income)
    
    // Macro with the explanatory variables
    local xvars "c1 c2 bmi1 bmi2 inc1 inc2 new1 new2 labin1 labin2 Cal Sug Sod Fat Price Serv Brand"
    
    set matastrict on
    mata:
    
    class nelogit scalar nl
    nl.setup(st_data(.,"`xvars'"), st_data(.,"choice"), 3)
    
    end
    In the initialization of the object (class), I get the following error and can't do anything else:

    Code:
     mata:
    ------------------------------------------------- mata (type end to exit) ------------------------------------------------------------------------------------------------
    : 
    : class nelogit scalar nl
    nothing found where '(' expected
    (3 lines skipped)
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    r(3000);
    
    end of do-file
    
    r(3000);
    Why am I getting this error? Is this not how you're supposed to initialize an object? Is it because I'm trying to use the object in interactive mode?
    Alfonso Sanchez-Penalver

  • #2
    Change the line

    Code:
     
     class nelogit scalar nl
    to

    Code:
     
     nl = nelogit()
    will fix the issue. The problem here is that in the Mata interactive mode, the variable can not be declared, the object has to be created.


    Comment


    • #3
      Thank you Hua Peng (StataCorp) !!! You know, this is confusing. As a long time object-oriented programmer in C# and Java I am very used to first declaring the variable, and then initializing it. I wish this was the case in Mata. I understand why we can not have a constructor with arguments, and that is okay because there is an easy workaround, but this is something that I don't really understand. I have always understood good practice to have to declare and initialize. I would very much prefer something like
      Code:
      nelogit scalar nl = [new] nelogit()
      or
      Code:
      nelogit scalar nl
      nl = [new] nelogit()
      independent of whether we have matastrict on or off. I put new in square brackets to make it optional. If you're going to use objects in your program, this is the way to do it independently of whether you are in interactive mode or within a function or another object (struct, class). I honestly don't understand why it is not.

      By the way, it also blows my mind that we have to put the keyword class in front when declaring in functions and other objects, which is why in my suggestion I don't include it. If nelogit is programmed as a class, Mata should know what type of object it is.

      Anyway thanks, and sorry for the ranting but it really makes no sense to me.
      Alfonso Sanchez-Penalver

      Comment


      • #4
        No problem, many of your suggestions are reasonable requests. Some of them are on our list of things to do. It's more how much resource/time we have kind of things.

        Comment

        Working...
        X