© Tyl Software Foundation 2019-2021
▶ RECORDS ASSIGNMENT
Assign Record by Another Record
Let's start with an experiment:
r1 {a 1}
r2 {}
r2 r1
print r2
{
a: 1
}
a: 1
}
Line
Let's see if
r2 r1
, is a record assignment statement, where r2
gets all mapping
data of r1
.
Let's see if
r2
is affected by changes in r1
:
r1 {a 1}
r2 {}
r2 r1
r1.a 10
print r1
print r2
{
a: 10
}
{
a: 1
}
a: 10
}
{
a: 1
}
r2
data is not affected, because the assignment does not construct
a reference between the record variables, meaning the assignment
action is only a data transfer from the source object to the target
object.
Data access in assigned record:
r1 {a 11 b 12}
r2 {a 21 c 23}
print 'r1: ' + r1
print 'r2 (before assignment): ' + r2
r2 r1
print 'r2 (after assignment): ' + r2
print
! static and dynamic access values of r2
print 'r2.a: ' + r2.a
print 'r2 `a`: ' + r2 'a'
print 'r2.b: ' + r2.b
print 'r2 `b`: ' + r2 'b'
r1: {
a: 11
b: 12
}
r2 (before assignment): {
a: 21
c: 23
}
r2 (after assignment): {
a: 11
c: 23
b: 12
}
r2.a: 11
r2 'a': 11
r2.b: NULL
r2 'b': 12
a: 11
b: 12
}
r2 (before assignment): {
a: 21
c: 23
}
r2 (after assignment): {
a: 11
c: 23
b: 12
}
r2.a: 11
r2 'a': 11
r2.b: NULL
r2 'b': 12
As can be seen in the results,
In this fix duration example:
r2
does not have a key 'b' in its declared
mapping data, nor has been added implicitly,
therefore r2.b
is not a valid record access
structure, and access should be done only in the format r2 'b'
.
In this fix duration example:
starttime endtime { totalsecond 0 }
testnow { totalsecond 127613.2075469 }
starttime testnow
testnow.totalsecond 127614.2215487
endtime testnow
print startspan starttime.totalsecond
print endspan endtime.totalsecond
print endspan - startspan
127613.2075469
127614.2215487
1.01400180000928
127614.2215487
1.01400180000928
In line '
It is good practice to let the target of record assignment have all properties that need to get data, declared in advance.
starttime endtime { totalsecond 0 }
', we declared two record variables
and assigned them a totalsecond
property with an initial value 0, then testnow
record that mimics a date & time utility is declared.
starttime
and endtime
are then assigned by testnow
, and get the current
totalsecond
. Because totalsecond
is present in the targets declarations mapping
data, it is possible to access them by dot notation.
It is good practice to let the target of record assignment have all properties that need to get data, declared in advance.