Copy on write semantics for collections #1565
anderson-joyle
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Introduced by PR #1512.
Set with value semantics
Collections are just global variables that support mutation. When changing a collection through a mutation function (Set, Collect, Patch, Remove, etc), Power Fx creates copies (shallow copy always) of in-memory global variables and collections
Examples
>>
Set(x, [1,2,3]
[1,2,3]
>>
Set(y, x)
// copy of 'x'[1,2,3]
>>
Collect(x, {Value:4})
{Value:4}
>>
Remove(x, {Value:2})
true
>>
x
// Changed. Expected.[1,3,4]
>>
y
// Unchanged. Expected.[1,2,3]
>>
Patch(y, First(y), {Value:99})
{Value:99}
>>
First(y)
{Value:99}
>>
First(x)
{Value:1}
The mutation functions return values already merged.
Deep mutation with Set
Set can modify fields and records deep within a data structure. In the following examples, it will produce a shallow copy of orig but reference is retained to individual records.
Examples
>>
Set( orig, [ {a:1, b:2}, {a:3, b:4} ] )
Table({a:1, b:2}, {a:3, b:4})
>>
Set( x, orig )
Table({a:1, b:2}, {a:3, b:4})
>>
Set( y, orig[1] )
{a:3, b:4}
>>
Set( orig[1].b, 9 )
9
>>
Notify( x[1].b )
2
>>
Notify( y.b )
2
Beta Was this translation helpful? Give feedback.
All reactions