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
 
(12 intermediate revisions by 3 users not shown)
Line 1: Line 1:
let is used to temporarily assign a value to a variable in an EAL expression.
<message>Write the content here to display this box</message>
Let is used to temporarily hold a value in an OCL and EAL expression.  


[[Derived attributes & associations|derived associations]] are used to create "shortcuts" in your model or to split complex navigations into parts.
Let can be very useful, but be careful when using it and also when deleting objects or referencing a derived expression.


Combining these two can lead to unexpected results.
=== Cause ===
It's '''not a variable, it's a reference'''.


==== Example with single link association ====
Also, look at [[Training:Derived attributes & associations|derived associations]] that are used to create "shortcuts" in your model or to split complex navigations into parts.
If you have an derived single link association called lastSubPart, derived like  
 
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
  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 22:
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 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.


So, every time you use '''lp''', the derived single link association is reevaluated.
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  
  (
  (
Line 24: Line 33:
   newPart.Name = 'copy of ' + lp.Name
   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'''.
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'''.
[[Category:Associations]]
{{Edited|July|12|2024}}

Latest revision as of 07:39, 20 September 2024

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

Let can be 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 0 days ago on 09/20/2024. What links here