An object of type Group, may have some members like values, and some other as pointers to values. Some objects are used as values also. A group object can be a value or a pointer to object.
Here we will see a subset of properties of groups about copy, deep and shallow.
About Check1 module:
We have a class named alfa which make a group of 4 members, a, b, k() and beta with member z. From these members objects are array k() and group beta. These are "values" for interpreter. See the add() which take a pointer to array one time and a double at second time and the semantic a+=m works for either type. The & symbol used for passing by reference.
About Check2 module:
Now our alfa class has k as pointer to array (we make it assign an empty array), and beta as pointer to group (we make it assign the null object, which is not Null but an object of type Null). We have a Class: part. This part say to interpreter that the following members exist only on construction time. Module alfa called when we use alfa() to make a new group of type alfa. Also we have a private class betaClass. A class definition which isn't member is global, but as member of group is local. So this class used to make the object which place to member beta. We do the work to place numbers to array which point the member k, and the object to beta from the constructor of alfa.
To use the z member of beta inner object we have to use the fat arrow. So for z, the z.beta=>z used for return or set value. See that between z and beta there is no fat arrow (this means that z isn't pointer to group, so beta is a normal member - a normal member can be pass by reference), So how we pass by reference the z member of beta? To do that we have to "open" the beta using for beta { }. At this point the block used for temporary definitions (all new definitions deleted after the exit from this block). So one temporary hidden variable is the variable which hold the group, not as pointer but as normal group. So tge add(&.z, 100) say that .z is member of the first hidden variable (we can use ten hidden variables, adding a list of variables one one or more nested for object blocks.
We see here that we have a shallow copy. The x group created from z but have the same values for k and beta (x get a copy of pointers, not a copy of those which points). So changing values from z side we get the same change from x side. When we do x=z we merge z to x, so we get copy from pointers not the copies of actual objects which these pointers points.
About Check3 module:
About Check4 module:
read m as alfa: let this=m
}
double x
set final {
read this as alfa
}
value final {
=this
}
class:
module alfa (.x) {}
}
z=alfa(100)
? z.x=100
m=alfa(300)
class beta {
x=500
}
try {
z=beta()
}
? z.x=100
z=m
? z.x=300
' let z=beta() skip the set part
let z=beta()
? z.x=500
? z is type alfa = true
' now z is also type beta
? z is type beta = true
k=beta()
k.x=1000
try {
z=k
} ' can't because now we not skip the Set part
About Check5 module:
add(&.k, 20)
for .beta {
add(&..z, 100)
}
}
b=(,)
? a is b = False
NullArray=(,)
a=NullArray
b=NullArray
? a is b = True
print module.name$
class alfa {
double a, b
dim k(1 to 10)=10
group beta {
z=100
}
}
z=alfa()
? z.k()#sum()=100 ' True
x=z ' deep copy because of k() and Group
add(&z.k(), 20)
add(&z.beta.z, 100)
? z.beta.z=200
? z.k()#sum()=300
? x.K()#sum()=100
? x.beta.z=100
x=z ' we do a merge and the values changed
? x.K()#sum()=300
? x.beta.z=200
sub add(&a, m)
a+=m
end sub
}
Check1
module Check2 {
print module.name$
class alfa {
double a, b
k=(,)
beta=pointer()
private:
class betaClass {
z
class:
module betaClass (.z) {
}
}
class:
module alfa {
dim a(1 to 10)=10
.k<=a()
.beta<=pointer(.betaClass(100))
}
}
z=alfa()
? z.k#sum()=100 ' True
x=z ' shallow copy because of k and beta are pointers to objects
add(&z.k, 20)
for z.beta {
add(&.z, 100)
}
? z.beta=>z=200
? z.k#sum()=300
? x.K#sum()=300
? x.beta=>z=200
? x.beta is z.beta = true
? x.k is z.k = true
? x is z = false
x=z ' we can do that, but objects but we have shallow copy too
? x is z = false ' x internal pointer can't change
' x and z are objects with unique and private pointer
' x and z deleted when this module exit run
sub add(&a, m)
a+=m
end sub
}
Check2
module Check3 {
print module.name$
class alfa {
double a, b
k=(,)
beta=pointer()
value {
m=this
m.beta=pointer(.betaClass(.beta=>z))
m.k=cons(.k)
=group(m)
}
private:
class betaClass {
z
class:
module betaClass (.z) {
}
}
class:
module alfa {
dim a(1 to 10)=10
.k<=a()
.beta<=pointer(.betaClass(100))
}
}
z=alfa()
? z.k#sum()=100 ' True
x=z ' deep copy because now object handle the export (has Value part)
add(&z.k, 20)
for z.beta {
add(&.z, 100)
}
? z.beta=>z=200
? z.k#sum()=300
? x.K#sum()=100
? x.beta=>z=100
// we can't change it because we didn't handle the import (has no Set part)
Try ok {
x=z
}
If not ok then Print "Error:";Error$
sub add(&a, m)
a+=m
end sub
}
Check3
module Check4 {
print module.name$
class alfa {
double a, b
k=(,)
beta=pointer()
value {
m=this
m.beta=pointer(.betaClass(.beta=>z))
m.k=cons(.k)
=group(m)
}
Set {
read this
}
private:
class betaClass {
z
class:
module betaClass (.z) {
}
}
class:
module alfa {
dim a(1 to 10)=10
.k<=a()
.beta<=pointer(.betaClass(100))
}
}
z=alfa()
? z.k#sum()=100 ' True
x=z ' deep copy because now object handle the export (has Value part)
add(&z.k, 20)
for z.beta {
add(&.z, 100)
}
? z.beta=>z=200
? z.k#sum()=300
? x.K#sum()=100
? x.beta=>z=100
' now we can merge z to x because we have a Set part
x=z
? x.K#sum()=300
? x.beta=>z=200
sub add(&a, m)
a+=m
end sub
}
Check4
module Check5 {
print module.name$
class alfa {
double a, b
k=(,)
beta=pointer()
value {
? "value ok"
m=this
m.beta=pointer(.betaClass(.beta=>z))
m.k=cons(.k)
=group(m)
}
Set {
read this
}
remove {
print "just deleted", .a, .b
}
private:
class betaClass {
z
class:
module betaClass (.z) {
}
}
class:
module alfa(.a, .b) {
dim a(1 to 10)=10
.k<=a()
.beta<=pointer(.betaClass(100))
}
}
z->alfa(10, 30)
? z=>k#sum()=100 ' True
' eval(z) call value when z is a pointer to group
x=eval(z) ' deep copy because now object handle the export (has Value part)
' x=group(z) 'shallow copy for members which are pointers to objects
for z {
add(&.k, 20)
for .beta {
add(&..z, 100)
}
}
? z=>beta=>z=200
? z=>k#sum()=300
? x.K#sum()=100
? x.beta=>z=100
? "now z will point to x, so old object deleted"
' z=pointer(x)
z->x
? "now z point to x"
? z=>beta=>z=100
? z=>k#sum()=100
? "now z point to a copy of x (as a deep copy)"
x.a+=100
z->(x)
for z {
add(&.k, 20)
for .beta {
add(&..z, 100)
}
}
? z=>a=110
? z=>beta=>z=200
? z=>k#sum()=300
? x.K#sum()=100
? x.beta=>z=100
? z is type alfa=true
? "now z point to null, old deleted"
z=pointer()
? z is type null=true
sub add(&a, m)
a+=m
end sub
}
Check5
Δεν υπάρχουν σχόλια:
Δημοσίευση σχολίου
You can feel free to write any suggestion, or idea on the subject.