Let and Derived associations
No edit summary
No edit summary
Line 1: Line 1:
Let is used to temporarily hold a value in an EAL expression.  
Let is used to temporarily hold a value in an EAL expression.  


Let is very useful, but be careful when using it and also deleting objects or referencing a derived expression.
Let is very useful, but be careful when using it and also when deleting objects or referencing a derived expression.


==== Cause ====
==== Cause ====
But, it's '''not a variable, it's a reference'''.  
It's '''not a variable, it's a reference'''.  


Also look at [[Derived attributes & associations|derived associations]] are used to create "shortcuts" in your model or to split complex navigations into parts.  
Also, look at [[Derived attributes & associations|derived associations]] that are used to create "shortcuts" in your model or to split complex navigations into parts.  


Both let refences and a combining these two can lead to unexpected results.
Both let references and combining these two can lead to unexpected results.


==== Example with single link association ====
==== Example with Single Link Association ====
If you have a derived single link association called '''lastSubPart''', derived like  
If you have a derived single link association called '''lastSubPart''', derived like this:
  self.subParts->last
  self.subParts->last
then you have a method with this content
then you have a method with this content:
  let lp = self.lastSubPart in  
  let lp = self.lastSubPart in  
  (
  (
Line 21: Line 21:
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 a reference to the '''subscription to the derivation to the object'''.
That's because '''lp''' is NOT holding '''the object -''' it's holding a reference to the '''subscription to the derivation to the object'''.


==== Solution ====
==== Solution ====

Revision as of 06:43, 13 March 2023

Let is used to temporarily hold a value in an EAL expression.

Let is very useful, but be careful when using it and also when deleting objects or referencing a derived expression.

Cause

It's not a variable, it's a reference.

Also, look at derived associations that are used to create "shortcuts" in your model or to split complex navigations into parts.

Both let references and 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 this:

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 reference to the subscription to the derivation to the object.

Solution

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 object itself, not the reference or subscription.

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