Skip to main content

OREFs

OREFs

The %New() method of an object class creates an internal, in-memory structure to contain the object’s data and returns an OREF (object reference) that points to that structure. An OREF is a special kind of value in InterSystems IRIS. You should remember the following points:

  • In the Terminal, the content of an OREF depends on the language in use:

    • In ObjectScript, you see a string that consists of a number, followed by an at sign (@), followed by the name of the class.

    • In Python, you see a string containing the class name and an 18 character unique location in memory.

    For example:

    TESTNAMESPACE>set myobj=##class(Sample.Person).%New()
     
    TESTNAMESPACE>w myobj
    3@Sample.Person 
    
    >>> myobj=iris.cls("Sample.Person")._New()
    >>> print(myobj)
    <iris.Sample.Person object at 0x000001A1E52FFD20>
    
  • InterSystems IRIS returns an error if you do not use an OREF where one is expected or you use one with an incorrect type. This error is different from the ObjectScript terminal and the Python terminal:

    TESTNAMESPACE>set x=2
    
    TESTNAMESPACE>set x.Name="Fred Parker"
    
    SET x.Name="Fred Parker"
    ^
    <INVALID OREF>
    
    >>> x=2
    >>> x.Name="Fred Parker"
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
    AttributeError: 'int' object has no attribute 'Name'
    

    It is helpful to be able to recognize this error. It means that the variable is not an OREF but should be.

  • There is only one way to create an OREF: use a method that returns an OREF. The methods that return OREFs are defined in the object classes or their subclasses.

    The following does not create an OREF, but rather a string that looks like an OREF:

    TESTNAMESPACE>set testthis="4@Sample.Person"
    
    >>> testthis="<iris.Sample.Person object at 0x000001A1E52FFD20>"
    
  • In ObjectScript, you can determine programmatically whether a variable contains an OREF. The function $IsObject returns 1 (true) if the variable contains an OREF; and it returns 0 (false) otherwise.

Note:

For persistent classes, methods such as %OpenId() also return OREFs.

FeedbackOpens in a new tab