Let and Derived associations
(Created page with "let is used to temporarily assign a value to a variable in an EAL expression. derived associations are used to create "shortcuts" in you...")
 
No edit summary
Line 6: Line 6:


==== Example with single link association ====
==== Example with single link association ====
If you have an derived single link association called lastSubPart, derived like  
If you have a derived single link association called '''lastSubPart''', derived like  
  self.subParts->last
  self.subParts->last
then you have a metod with this content
then you have a method with this content
  let lp = self.lastSubPart in  
  let lp = self.lastSubPart in  
  (
  (
Line 16: Line 16:
You might expect newPart.Name to hold 'copy of <name of the lastsubpart>', but it doesn't.
You might expect newPart.Name to hold 'copy of <name of the lastsubpart>', but it doesn't.


That's because '''lp''' is NOT holding '''the object''', it's holding the '''subscription to the object'''.
That's because '''lp''' is NOT holding '''the object''', it's holding a '''subscription to the derivation to the object'''.


So, every time you use '''lp''', the derived single link association is reevaluated.
When the code does ''self.subParts.add(newPart)'', '''lp''' changes to point to the new object.
 
I.e. every time you use '''lp''', the derived single link association is reevaluated.
  let lp = self.lastSubPart'''->first''' in  
  let lp = self.lastSubPart'''->first''' in  
  (
  (

Revision as of 16:41, 30 April 2020

let is used to temporarily assign a value to a variable in an EAL expression.

derived associations are used to create "shortcuts" in your model or to split complex navigations into parts.

Combining these two can lead to unexpected results.

Example with single link association

If you have a derived single link association called lastSubPart, derived like

self.subParts->last

then you have a method with this content

let lp = self.lastSubPart in 
(
  self.subParts.add(newPart);
  newPart.Name = 'copy of ' + lp.Name
)

You might expect newPart.Name to hold 'copy of <name of the lastsubpart>', but it doesn't.

That's because lp is NOT holding the object, it's holding a subscription to the derivation to the object.

When the code does self.subParts.add(newPart), lp changes to point to the new object.

I.e. every time you use lp, the derived single link association is reevaluated.

let lp = self.lastSubPart->first in 
(
  self.subParts.add(newPart);
  newPart.Name = 'copy of ' + lp.Name
)

The first operator here converts the object to a set and then takes the first, which is the same object, but breaks the subscription.

This page was edited 85 days ago on 02/10/2024. What links here