Question
· Mar 23, 2018

No error when referencing unset relationship object property

I've been writing ObjectScript for a long time and was a little surprised by this behavior so I'd like to understand what is going on.

I was doing a peer review on some source which had roughly the equivalent of the following line:

Set myRecord = ##class(MyObj.Record).%OpenId(id)
Set emailList = previousEmails_","_myRecord.RecordGroup.EmailList

I flagged it for further testing because the code didn't check if RecordGroup was set and I knew that not all records were part of a Group so I was expecting that in cases where it wasn't set this line should throw an <INVALID OREF>

However, it turns out that I had forgotten that in the MyObj.Record class, RecordGroup is actually a one:many relationship: 

 Relationship RecordGroup As MyObj.MyGroup [ Cardinality = one, Inverse = Records ];

This results in the behavior that I didn't expect .... when there is no relationship set up, myRecord.RecordGroup.EmailList returns "" and not an error.  In fact, myRecord.RecordGroup.PropertyNotEvenDefinedInMyGroupClassDef also returns "".

Can anyone explain this behavior?  I am testing on 2016.1.

Discussion (2)1
Log in or sign up to continue

The behavior you are seeing is because of chain '.' handling of null objects. For example if you:

Set person=##class(Sample.Person).%New()

Write person.Name.AnythingYouLike

It will succeed and return "", but if you:

Set tmp=person.Name

Write tmp.AnythingYouLike

It will fail with an INVALID OREF error, as would 'Write (person.Name).AnythingYouLike'.

This behavior is inconsistent, so I will not defend it, but it is how the product works.