Immutability




Question: After `Person.allInstances->orderBy(name) (ExpressionHandle.Element as IObjectList).AddNew() throw the Exception: System.NotSupportedException. Is this correct behaviour?

Answer: Most ocl list-operations will create a new list rather than manipulating the orignal one. The new list is readonly and does not have any connection to the original list. It does not know how to perform an AddNew operation. The same would have happened if the original list had been an association end. Imagine for example the expression:

 Person.allInstances->select(assets > 10000)

If you would try to add a new object to this list, it would have to have the attribute assets set to a value that would qualify in the condition.

In the case of ordering, the AddNew would have to take into account that the list is ordered (something that the list itself is unaware of) in order to add the new object in the correct possition.

The solution is to add the new object to a list that is not filtered/ordered, and it will appear in the correct position in the ordered list in the handle (thanks to subscriptions)