Announcement

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

  • Mata constructor with arguments

    Hi
    When building classes in Mata it seems like one needs to instantiate the class before setting it's properties (see example coord below which works).
    There is a constructor (new) but as the documentation says it can't take any arguments.

    I would like to do it like in class coord2 below where instantiation and setting of properties is done in 1 line.

    Is this possible?


    Code:
    cls
    mata
    mata clear
    mata describe
    
    
    class coord {
        real scalar    x, y
        void setup()
    }
    
        void coord::setup(real scalar x, y)
        {
            this.x = x
            this.y = y
        }
    
        
    xy = coord()
    xy.setup(1, 2)
    
    xy.x
    xy.y
    
    
    class coord2 {
        real scalar x, y
        void new()
    }
    
        void coord2::new(real scalar x, y)
        {
            this.x = x
            this.y = y
        }
    
        
    xy2 = coord2(1, 2)
    
    xy2.x
    xy2.y
    end
    Kind regards

    nhb

  • #2
    Niels Henrik Bruun the same issue persists in version 14. It would definitely make life easier if it were possible to pass arguments to constructors.

    Comment


    • #3
      Niels Henrik Bruun and wbuchanan :

      I agree that this would be nice. While it takes a little bit more work, one can proceed as Niels Henrik Bruun initially suggests, and then make a wrapper that does everything in one step. This is my coordmaker() function below.
      Code:
      mata
      class coord {
          real scalar    x, y
          void setup()
      }
      
          void coord::setup(real scalar x, y)
          {
              this.x = x
              this.y = y
          }
      
      class coord coordmaker(x,y)
      {
          class coord scalar xy
          xy=coord()
          xy.setup(x,y)
          return(xy)
      
      }    
      xy = coord()
      xy.setup(1, 2)
      
      xy.x
      xy.y
      
      /* One step */
      yz=coordmaker(2,3)
      yz.x
      yz.y
      end
      Hope that helps!

      Matt

      Comment


      • #4
        Matthew J. Baker I ended up having to do something similar. It's just a bit of a pain in terms of introducing additional points of failure. In my case in particular, it's a bit more of a pain since I'm trying to wrap an existing library in a different language that keeps the native API as intact as possible (and then will build out Stata interfaces to the Mata classes/methods afterwards).

        Comment


        • #5
          Matthew J. Baker : Thank you for your help. I ended up deciding not to use the function approach.
          Kind regards

          nhb

          Comment


          • #6
            Matthew J. Baker have you had any success accessing/chaining methods/functions (http://www.statalist.org/forums/foru...ion-chaining)? I have a larger class defined with methods that generate other classes, which also have their methods. Structs didn't seem like a plausible option since there are several occasions where same named methods exist for different classes and use different arguments/logic. I'm kind of stumped at this point since it wouldn't be that difficult to do something similar in Java and/or other languages, but was trying to keep things in Mata since there seems to be less control when things come of the stack with the Java API.

            Comment


            • #7
              wbuchanan --

              Sorry I just saw your last message today as I have been unavoidably detained by other matters. I couldn't get the link you posted to work is it possible to repost it?

              Best,

              Matt

              Comment


              • #8
                Hi Matthew J. Baker,

                The link was to a related post about method/function chaining in mata:

                http://www.statalist.org/forums/foru...ction-chaining

                Comment

                Working...
                X