Announcement

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

  • Class programming

    Dear All

    When writing a Stata code I want to dump the description of the current object.

    If the object already exists, I can do it with:

    Code:
    classutil des .myobj
    But now I want to do it in the constructor, and at that point Stata doesn't seem to allow to refer to the object by name yet:

    Code:
    myobj:  object not found
    r(111);
    Also, a solution could have been a special alias word, present in other languages, like "this" in C#, or "self" in Python. But there seems to be no equivalent in Stata.

    Various AI models either indicated that there is no equivalent or suggested trying "classutil des ." which of course doesn't work:

    Code:
    "." invalid object name
    r(198);
    Any advice or a workaround?

  • #2
    I hesitate to make this suggestion, but I think this may be what you're looking for, so I wanted to share my thoughts.
    You can use the current object's key:

    ```stata
    classutil describe .`.objkey'
    ```

    The built-in `.objkey`, without an object name before it, returns the key of the implied context. Inside `.new`, that context is the instance being constructed. This allows inspection before the assignment to the caller's object name has completed.

    For example, save the following as `example.class` on the ado-path or in the current directory:

    ```stata
    version 16.0
    class example {
    double x = 42
    string note = "inside constructor"
    }

    program .new
    classutil describe .`.objkey'
    end
    ```

    Then run:

    ```stata
    .myobj = .example.new
    ```

    The description is printed from inside the constructor. The displayed identifier has the form `.__key(...)`; its value is session dependent.

    Equivalently, you can use a local macro:

    ```stata
    local self "`.objkey'"
    classutil describe .`self'
    ```

    Here, `self` is simply a macro name, not a reserved keyword.

    I verified this in Stata/MP 16.1, including nested objects, an inherited constructor, and an overridden constructor calling `.Super.new`. A child method sees the child object's key; the base constructor invoked through `.Super.new` sees the same derived instance.

    See [Stata's class manual, section 12.1, Keys](https://www.stata.com/manuals/pclass.pdf#page=30).

    Cheers!
    Choonjoo

    Comment


    • #3

      If this is what your are looking for, you can use .objkey to refer to the current object inside its constructor.

      Save the following as example.class in Stata's current working directory (type pwd to check):

      Code:
      version 16.0
      
      class example {
          double x = 42
          string note = "inside constructor"
      }
      
      program .new
          classutil describe .`.objkey'
      end
      Then run the following in Stata:

      Code:
      .myobj = .example.new

      Comment

      Working...
      X