Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -3,6 +3,7 @@ with Ada.Text_IO; use Ada.Text_IO;
|
|||
procedure Test_Polymorphic_Copy is
|
||||
package Base is
|
||||
type T is tagged null record;
|
||||
type T_ptr is access all T'Class;
|
||||
function Name (X : T) return String;
|
||||
end Base;
|
||||
use Base;
|
||||
|
|
@ -21,6 +22,12 @@ procedure Test_Polymorphic_Copy is
|
|||
Put_Line ("Copied " & Duplicate.Name); -- Check the copy
|
||||
end Copier;
|
||||
|
||||
-- The function knows nothing about S and creates a copy on the heap
|
||||
function Clone (X : T'Class) return T_ptr is
|
||||
begin
|
||||
return new T'Class(X);
|
||||
end Copier;
|
||||
|
||||
package Derived is
|
||||
type S is new T with null record;
|
||||
overriding function Name (X : S) return String;
|
||||
|
|
@ -36,7 +43,11 @@ procedure Test_Polymorphic_Copy is
|
|||
|
||||
Object_1 : T;
|
||||
Object_2 : S;
|
||||
Object_3 : T_ptr := Clone(T);
|
||||
Object_4 : T_ptr := Clone(S);
|
||||
begin
|
||||
Copier (Object_1);
|
||||
Copier (Object_2);
|
||||
Put_Line ("Cloned " & Object_3.all.Name);
|
||||
Put_Line ("Cloned " & Object_4.all.Name);
|
||||
end Test_Polymorphic_Copy;
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ class S : T {
|
|||
}
|
||||
|
||||
override string toString() {
|
||||
return "I'm the instance of S p: " ~ cast(string)str;
|
||||
return "I'm the instance of S p: " ~ str.idup;
|
||||
}
|
||||
|
||||
override T duplicate() { return new S(this); }
|
||||
|
|
@ -41,6 +41,6 @@ void main () {
|
|||
T copy = orig.duplicate();
|
||||
orig.custom('Y');
|
||||
|
||||
writeln(orig);
|
||||
writeln(copy); // Should have 'X' at the beginning.
|
||||
orig.writeln;
|
||||
copy.writeln; // Should have 'X' at the beginning.
|
||||
}
|
||||
|
|
|
|||
14
Task/Polymorphic-copy/Groovy/polymorphic-copy-1.groovy
Normal file
14
Task/Polymorphic-copy/Groovy/polymorphic-copy-1.groovy
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
class T implements Cloneable {
|
||||
String property
|
||||
String name() { 'T' }
|
||||
T copy() {
|
||||
try { super.clone() }
|
||||
catch(CloneNotSupportedException e) { null }
|
||||
}
|
||||
@Override
|
||||
boolean equals(that) { this.name() == that?.name() && this.property == that?.property }
|
||||
}
|
||||
|
||||
class S extends T {
|
||||
@Override String name() { 'S' }
|
||||
}
|
||||
14
Task/Polymorphic-copy/Groovy/polymorphic-copy-2.groovy
Normal file
14
Task/Polymorphic-copy/Groovy/polymorphic-copy-2.groovy
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
T obj1 = new T(property: 'whatever')
|
||||
S obj2 = new S(property: 'meh')
|
||||
|
||||
def objA = obj1.copy()
|
||||
def objB = obj2.copy()
|
||||
|
||||
assert objA.class == T
|
||||
assert objA == obj1 && ! objA.is(obj1) // same values, not same instance
|
||||
|
||||
assert objB.class == S
|
||||
assert objB == obj2 && ! objB.is(obj2) // same values, not same instance
|
||||
|
||||
println "objA:: name: ${objA.name()}, property: ${objA.property}"
|
||||
println "objB:: name: ${objB.name()}, property: ${objB.property}"
|
||||
Loading…
Add table
Add a link
Reference in a new issue