Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
53
Task/Polymorphic-copy/Ada/polymorphic-copy.adb
Normal file
53
Task/Polymorphic-copy/Ada/polymorphic-copy.adb
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
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;
|
||||
|
||||
package body Base is
|
||||
function Name (X : T) return String is
|
||||
begin
|
||||
return "T";
|
||||
end Name;
|
||||
end Base;
|
||||
|
||||
-- The procedure knows nothing about S
|
||||
procedure Copier (X : T'Class) is
|
||||
Duplicate : T'Class := X; -- A copy of X
|
||||
begin
|
||||
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 Clone;
|
||||
|
||||
package Derived is
|
||||
type S is new T with null record;
|
||||
overriding function Name (X : S) return String;
|
||||
end Derived;
|
||||
use Derived;
|
||||
|
||||
package body Derived is
|
||||
function Name (X : S) return String is
|
||||
begin
|
||||
return "S";
|
||||
end Name;
|
||||
end Derived;
|
||||
|
||||
Object_1 : T;
|
||||
Object_2 : S;
|
||||
Object_3 : T_ptr := Clone(Object_1);
|
||||
Object_4 : T_ptr := Clone(Object_2);
|
||||
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;
|
||||
70
Task/Polymorphic-copy/V-(Vlang)/polymorphic-copy.v
Normal file
70
Task/Polymorphic-copy/V-(Vlang)/polymorphic-copy.v
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
interface Iden {
|
||||
identify() string
|
||||
}
|
||||
|
||||
struct Tina {
|
||||
value f64
|
||||
}
|
||||
|
||||
// structs can be embedded
|
||||
|
||||
struct Sara {
|
||||
tina Tina
|
||||
koan string
|
||||
}
|
||||
|
||||
struct Ryan {
|
||||
tina Tina
|
||||
ch chan int
|
||||
}
|
||||
|
||||
// methods assigned to structs
|
||||
|
||||
fn (x Tina) identify() string {
|
||||
return "I'm a Tina!"
|
||||
}
|
||||
|
||||
fn (x Sara) identify() string {
|
||||
return "I'm a Sara!"
|
||||
}
|
||||
|
||||
fn (x Ryan) identify() string {
|
||||
return x.tina.identify()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// initialization of three variables with different types
|
||||
tina1 := Tina{5}
|
||||
sara1 := Sara{Tina{6}, "one"}
|
||||
ryan1 := Ryan{Tina{7}, chan int{}}
|
||||
|
||||
// variables declared with the same interface type
|
||||
mut i1, mut i2, mut i3 := []Iden{}, []Iden{}, []Iden{}
|
||||
println("Initial (zero) values of interface variables:")
|
||||
println("i1: ${i1}")
|
||||
println("i2: ${i2}")
|
||||
println("i3: ${i3}")
|
||||
|
||||
// i1, i2, and i3 have the static type of the interface, along with other different types.
|
||||
i1 << [tina1]; i2 << [sara1]; i3 << [ryan1] // semicolons can be used as optional separators in Vlang
|
||||
println("\nPolymorphic:")
|
||||
println("i1: ${i1} / ${i1[0].identify()} / ${typeof(i1[0]).name}\n")
|
||||
println("i2: ${i2} / ${i2[0].identify()} / ${typeof(i2[0]).name}\n")
|
||||
println("i3: ${i3} / ${i3[0].identify()} / ${typeof(i3[0]).name}\n")
|
||||
|
||||
// copy: declare and assign in one step using "short declaration"
|
||||
i1c, i2c, i3c := i1, i2, i3
|
||||
|
||||
// modify first set of polymorphic variables.
|
||||
i1, i2, i3 = [Sara{Tina{3}, "dog"}], [Ryan{Tina{1}, chan int{}}], [Tina{2}]
|
||||
|
||||
println("\nFirst set now modified:")
|
||||
println("i1: ${i1} / ${i1[0].identify()} / ${typeof(i1).name}\n")
|
||||
println("i2: ${i2} / ${i2[0].identify()} / ${typeof(i2).name}\n")
|
||||
println("i3: ${i3} / ${i3[0].identify()} / ${typeof(i3).name}\n")
|
||||
|
||||
println("\nCopies made before modifications:")
|
||||
println("i1c: ${i1c} / ${i1c[0].identify()} / ${typeof(i1c).name}\n")
|
||||
println("i2c: ${i2c} / ${i2c[0].identify()} / ${typeof(i2c).name}\n")
|
||||
println("i3c: ${i3c} / ${i3c[0].identify()} / ${typeof(i3c).name}\n")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue