WeixiangYan/CodeTransOcean huggingface dataset:
Task: Polymorphic copy





Java: class T implements Cloneable {
    public String name() { return "T"; }
    public T copy() {
        try {
            return (T)super.clone();
        } catch (CloneNotSupportedException e) {
            return null;
        }
    }
}

class S extends T {
    public String name() { return "S"; }
}

public class PolymorphicCopy {
    public static T copier(T x) { return x.copy(); }
    public static void main(String[] args) {
        T obj1 = new T();
        S obj2 = new S();
        System.out.println(copier(obj1).name()); 
        System.out.println(copier(obj2).name()); 
    }
}







Ada: 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;
   
      
   procedure Copier (X : T'Class) is
      Duplicate : T'Class := X;  
   begin
      Put_Line ("Copied " & Duplicate.Name); 
   end Copier;

      
   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;
   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(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;
Task: Polymorphic copy







Python: import copy

class T:
   def classname(self): 
      return self.__class__.__name__

   def __init__(self):
      self.myValue = "I'm a T."

   def speak(self):
      print self.classname(), 'Hello', self.myValue

   def clone(self):
      return copy.copy(self)

class S1(T):
   def speak(self):
      print self.classname(),"Meow", self.myValue

class S2(T):
   def speak(self):
      print self.classname(),"Woof", self.myValue


print "creating initial objects of types S1, S2, and T"
a = S1()
a.myValue = 'Green'
a.speak()

b = S2()
b.myValue = 'Blue'
b.speak()

u = T()
u.myValue = 'Purple'
u.speak()

print "Making copy of a as u, colors and types should match"
u = a.clone()
u.speak()
a.speak()
print "Assigning new color to u, A's color should be unchanged."
u.myValue = "Orange"
u.speak()
a.speak()

print "Assigning u to reference same object as b, colors and types should match"
u = b
u.speak()
b.speak()
print "Assigning new color to u. Since u,b references same object b's color changes as well"
u.myValue = "Yellow"
u.speak()
b.speak()





Ada: 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;
   
      
   procedure Copier (X : T'Class) is
      Duplicate : T'Class := X;  
   begin
      Put_Line ("Copied " & Duplicate.Name); 
   end Copier;

      
   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;
   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(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;
Task: Polymorphic copy

C: int x;
int y = x;
Task: Polymorphic copy



C#: using System;

class T
{
    public virtual string Name()
    {
        return "T";
    }

    public virtual T Clone()
    {
        return new T();
    }
}

class S : T
{
    public override string Name()
    {
        return "S";
    }

    public override T Clone()
    {
        return new S();
    }
}

class Program
{
    static void Main()
    {
        T original = new S();
        T clone = original.Clone();

        Console.WriteLine(original.Name());
        Console.WriteLine(clone.Name());
    }
}
Task: Polymorphic copy


C++: #include <iostream>

class T
{
public:
  virtual void identify() { std::cout << "I am a genuine T" << std::endl; }
  virtual T* clone() { return new T(*this); }
  virtual ~T() {}
};

class S: public T
{
public:
  virtual void identify() { std::cout << "I am an S" << std::endl; }
  virtual S* clone() { return new S(*this); }
};

class X 
{
public:
  
  X(T* t): member(t) {}

  
  X(X const& other): member(other.member->clone()) {}

  
  X& operator=(X const& other)
  {
    T* new_member = other.member->clone();
    delete member;
    member = new_member;
  }

  
  ~X() { delete member; }

  
  void identify_member() { member->identify(); }

private:
  T* member;
};

int main()
{
  X original(new S);      
  X copy = original;      
  copy.identify_member(); 
}
Task: Polymorphic copy





Java: class T implements Cloneable {
    public String name() { return "T"; }
    public T copy() {
        try {
            return (T)super.clone();
        } catch (CloneNotSupportedException e) {
            return null;
        }
    }
}

class S extends T {
    public String name() { return "S"; }
}

public class PolymorphicCopy {
    public static T copier(T x) { return x.copy(); }
    public static void main(String[] args) {
        T obj1 = new T();
        S obj2 = new S();
        System.out.println(copier(obj1).name()); 
        System.out.println(copier(obj2).name()); 
    }
}
Task: Polymorphic copy







Python: import copy

class T:
   def classname(self): 
      return self.__class__.__name__

   def __init__(self):
      self.myValue = "I'm a T."

   def speak(self):
      print self.classname(), 'Hello', self.myValue

   def clone(self):
      return copy.copy(self)

class S1(T):
   def speak(self):
      print self.classname(),"Meow", self.myValue

class S2(T):
   def speak(self):
      print self.classname(),"Woof", self.myValue


print "creating initial objects of types S1, S2, and T"
a = S1()
a.myValue = 'Green'
a.speak()

b = S2()
b.myValue = 'Blue'
b.speak()

u = T()
u.myValue = 'Purple'
u.speak()

print "Making copy of a as u, colors and types should match"
u = a.clone()
u.speak()
a.speak()
print "Assigning new color to u, A's color should be unchanged."
u.myValue = "Orange"
u.speak()
a.speak()

print "Assigning u to reference same object as b, colors and types should match"
u = b
u.speak()
b.speak()
print "Assigning new color to u. Since u,b references same object b's color changes as well"
u.myValue = "Yellow"
u.speak()
b.speak()
Task: Polymorphic copy




Go: package main

import (
    "fmt"
    "reflect"
)


type i interface {
    identify() string
}


type t float64





type s struct {
    t
    kōan string
}



type r struct {
    t
    ch chan int
}




func (x t) identify() string {
    return "I'm a t!"
}




func (x s) identify() string {
    return "I'm an s!"
}

func main() {
    
    var t1 t = 5
    var s1 s = s{6, "one"}
    var r1 r = r{t: 7}

    
    var i1, i2, i3 i
    fmt.Println("Initial (zero) values of interface variables:")
    fmt.Println("i1:", i1)
    fmt.Println("i2:", i2)
    fmt.Println("i3:", i3)

    
    
    i1, i2, i3 = t1, s1, r1
    fmt.Println("\nPolymorphic:")
    fmt.Println("i1:", i1, "/", i1.identify(), "/", reflect.TypeOf(i1))
    fmt.Println("i2:", i2, "/", i2.identify(), "/", reflect.TypeOf(i2))
    fmt.Println("i3:", i3, "/", i3.identify(), "/", reflect.TypeOf(i3))

    
    i1c, i2c, i3c := i1, i2, i3

    
    i1, i2, i3 = s{3, "dog"}, r{t: 1}, t(2)

    
    
    fmt.Println("\nFirst set now modified:")
    fmt.Println("i1:", i1, "/", i1.identify(), "/", reflect.TypeOf(i1))
    fmt.Println("i2:", i2, "/", i2.identify(), "/", reflect.TypeOf(i2))
    fmt.Println("i3:", i3, "/", i3.identify(), "/", reflect.TypeOf(i3))

    fmt.Println("\nCopies made before modifications:")
    fmt.Println("i1c:", i1c, "/", i1c.identify(), "/", reflect.TypeOf(i1c))
    fmt.Println("i2c:", i2c, "/", i2c.identify(), "/", reflect.TypeOf(i2c))
    fmt.Println("i3c:", i3c, "/", i3c.identify(), "/", reflect.TypeOf(i3c))
}
Task: Polymorphic copy



C#: using System;

class T
{
    public virtual string Name()
    {
        return "T";
    }

    public virtual T Clone()
    {
        return new T();
    }
}

class S : T
{
    public override string Name()
    {
        return "S";
    }

    public override T Clone()
    {
        return new S();
    }
}

class Program
{
    static void Main()
    {
        T original = new S();
        T clone = original.Clone();

        Console.WriteLine(original.Name());
        Console.WriteLine(clone.Name());
    }
}








Fortran: 


module polymorphic_copy_example_module
   implicit none
   private 
   public :: T,S

   type, abstract :: T
   contains
      procedure (T_procedure1), deferred, pass :: identify
      procedure (T_procedure2), deferred, pass :: duplicate
   end type T


   abstract interface
      subroutine T_procedure1(this)
         import  :: T
         class(T), intent(inout) :: this
      end subroutine T_procedure1
      function T_procedure2(this) result(Tobj)
         import  :: T
         class(T), intent(inout) :: this
         class(T), allocatable :: Tobj
      end function T_procedure2
   end interface

   type, extends(T) :: S
   contains
      procedure, pass :: identify
      procedure, pass :: duplicate
   end type S

 contains

   subroutine  identify(this)
      implicit none
      class(S), intent(inout) :: this
      write(*,*) "S"
   end subroutine identify

   function duplicate(this) result(obj)
      class(S), intent(inout) :: this
      class(T), allocatable :: obj
      allocate(obj, source = S())
   end function duplicate

end module polymorphic_copy_example_module




program    test
   use polymorphic_copy_example_module
   implicit none

   class(T), allocatable :: Sobj
   class(T), allocatable :: Sclone

   allocate(Sobj, source = S())
   allocate(Sclone, source = Sobj % duplicate())

   call Sobj % identify()
   call Sclone % identify()

end program test
Task: Polymorphic copy


C++: #include <iostream>

class T
{
public:
  virtual void identify() { std::cout << "I am a genuine T" << std::endl; }
  virtual T* clone() { return new T(*this); }
  virtual ~T() {}
};

class S: public T
{
public:
  virtual void identify() { std::cout << "I am an S" << std::endl; }
  virtual S* clone() { return new S(*this); }
};

class X 
{
public:
  
  X(T* t): member(t) {}

  
  X(X const& other): member(other.member->clone()) {}

  
  X& operator=(X const& other)
  {
    T* new_member = other.member->clone();
    delete member;
    member = new_member;
  }

  
  ~X() { delete member; }

  
  void identify_member() { member->identify(); }

private:
  T* member;
};

int main()
{
  X original(new S);      
  X copy = original;      
  copy.identify_member(); 
}









Fortran: 


module polymorphic_copy_example_module
   implicit none
   private 
   public :: T,S

   type, abstract :: T
   contains
      procedure (T_procedure1), deferred, pass :: identify
      procedure (T_procedure2), deferred, pass :: duplicate
   end type T


   abstract interface
      subroutine T_procedure1(this)
         import  :: T
         class(T), intent(inout) :: this
      end subroutine T_procedure1
      function T_procedure2(this) result(Tobj)
         import  :: T
         class(T), intent(inout) :: this
         class(T), allocatable :: Tobj
      end function T_procedure2
   end interface

   type, extends(T) :: S
   contains
      procedure, pass :: identify
      procedure, pass :: duplicate
   end type S

 contains

   subroutine  identify(this)
      implicit none
      class(S), intent(inout) :: this
      write(*,*) "S"
   end subroutine identify

   function duplicate(this) result(obj)
      class(S), intent(inout) :: this
      class(T), allocatable :: obj
      allocate(obj, source = S())
   end function duplicate

end module polymorphic_copy_example_module




program    test
   use polymorphic_copy_example_module
   implicit none

   class(T), allocatable :: Sobj
   class(T), allocatable :: Sclone

   allocate(Sobj, source = S())
   allocate(Sclone, source = Sobj % duplicate())

   call Sobj % identify()
   call Sclone % identify()

end program test
Task: Polymorphic copy

C: int x;
int y = x;










Fortran: 


module polymorphic_copy_example_module
   implicit none
   private 
   public :: T,S

   type, abstract :: T
   contains
      procedure (T_procedure1), deferred, pass :: identify
      procedure (T_procedure2), deferred, pass :: duplicate
   end type T


   abstract interface
      subroutine T_procedure1(this)
         import  :: T
         class(T), intent(inout) :: this
      end subroutine T_procedure1
      function T_procedure2(this) result(Tobj)
         import  :: T
         class(T), intent(inout) :: this
         class(T), allocatable :: Tobj
      end function T_procedure2
   end interface

   type, extends(T) :: S
   contains
      procedure, pass :: identify
      procedure, pass :: duplicate
   end type S

 contains

   subroutine  identify(this)
      implicit none
      class(S), intent(inout) :: this
      write(*,*) "S"
   end subroutine identify

   function duplicate(this) result(obj)
      class(S), intent(inout) :: this
      class(T), allocatable :: obj
      allocate(obj, source = S())
   end function duplicate

end module polymorphic_copy_example_module




program    test
   use polymorphic_copy_example_module
   implicit none

   class(T), allocatable :: Sobj
   class(T), allocatable :: Sclone

   allocate(Sobj, source = S())
   allocate(Sclone, source = Sobj % duplicate())

   call Sobj % identify()
   call Sclone % identify()

end program test
Task: Polymorphic copy




Go: package main

import (
    "fmt"
    "reflect"
)


type i interface {
    identify() string
}


type t float64





type s struct {
    t
    kōan string
}



type r struct {
    t
    ch chan int
}




func (x t) identify() string {
    return "I'm a t!"
}




func (x s) identify() string {
    return "I'm an s!"
}

func main() {
    
    var t1 t = 5
    var s1 s = s{6, "one"}
    var r1 r = r{t: 7}

    
    var i1, i2, i3 i
    fmt.Println("Initial (zero) values of interface variables:")
    fmt.Println("i1:", i1)
    fmt.Println("i2:", i2)
    fmt.Println("i3:", i3)

    
    
    i1, i2, i3 = t1, s1, r1
    fmt.Println("\nPolymorphic:")
    fmt.Println("i1:", i1, "/", i1.identify(), "/", reflect.TypeOf(i1))
    fmt.Println("i2:", i2, "/", i2.identify(), "/", reflect.TypeOf(i2))
    fmt.Println("i3:", i3, "/", i3.identify(), "/", reflect.TypeOf(i3))

    
    i1c, i2c, i3c := i1, i2, i3

    
    i1, i2, i3 = s{3, "dog"}, r{t: 1}, t(2)

    
    
    fmt.Println("\nFirst set now modified:")
    fmt.Println("i1:", i1, "/", i1.identify(), "/", reflect.TypeOf(i1))
    fmt.Println("i2:", i2, "/", i2.identify(), "/", reflect.TypeOf(i2))
    fmt.Println("i3:", i3, "/", i3.identify(), "/", reflect.TypeOf(i3))

    fmt.Println("\nCopies made before modifications:")
    fmt.Println("i1c:", i1c, "/", i1c.identify(), "/", reflect.TypeOf(i1c))
    fmt.Println("i2c:", i2c, "/", i2c.identify(), "/", reflect.TypeOf(i2c))
    fmt.Println("i3c:", i3c, "/", i3c.identify(), "/", reflect.TypeOf(i3c))
}







Fortran: 


module polymorphic_copy_example_module
   implicit none
   private 
   public :: T,S

   type, abstract :: T
   contains
      procedure (T_procedure1), deferred, pass :: identify
      procedure (T_procedure2), deferred, pass :: duplicate
   end type T


   abstract interface
      subroutine T_procedure1(this)
         import  :: T
         class(T), intent(inout) :: this
      end subroutine T_procedure1
      function T_procedure2(this) result(Tobj)
         import  :: T
         class(T), intent(inout) :: this
         class(T), allocatable :: Tobj
      end function T_procedure2
   end interface

   type, extends(T) :: S
   contains
      procedure, pass :: identify
      procedure, pass :: duplicate
   end type S

 contains

   subroutine  identify(this)
      implicit none
      class(S), intent(inout) :: this
      write(*,*) "S"
   end subroutine identify

   function duplicate(this) result(obj)
      class(S), intent(inout) :: this
      class(T), allocatable :: obj
      allocate(obj, source = S())
   end function duplicate

end module polymorphic_copy_example_module




program    test
   use polymorphic_copy_example_module
   implicit none

   class(T), allocatable :: Sobj
   class(T), allocatable :: Sclone

   allocate(Sobj, source = S())
   allocate(Sclone, source = Sobj % duplicate())

   call Sobj % identify()
   call Sclone % identify()

end program test
Task: Polymorphic copy





Java: class T implements Cloneable {
    public String name() { return "T"; }
    public T copy() {
        try {
            return (T)super.clone();
        } catch (CloneNotSupportedException e) {
            return null;
        }
    }
}

class S extends T {
    public String name() { return "S"; }
}

public class PolymorphicCopy {
    public static T copier(T x) { return x.copy(); }
    public static void main(String[] args) {
        T obj1 = new T();
        S obj2 = new S();
        System.out.println(copier(obj1).name()); 
        System.out.println(copier(obj2).name()); 
    }
}






Fortran: 


module polymorphic_copy_example_module
   implicit none
   private 
   public :: T,S

   type, abstract :: T
   contains
      procedure (T_procedure1), deferred, pass :: identify
      procedure (T_procedure2), deferred, pass :: duplicate
   end type T


   abstract interface
      subroutine T_procedure1(this)
         import  :: T
         class(T), intent(inout) :: this
      end subroutine T_procedure1
      function T_procedure2(this) result(Tobj)
         import  :: T
         class(T), intent(inout) :: this
         class(T), allocatable :: Tobj
      end function T_procedure2
   end interface

   type, extends(T) :: S
   contains
      procedure, pass :: identify
      procedure, pass :: duplicate
   end type S

 contains

   subroutine  identify(this)
      implicit none
      class(S), intent(inout) :: this
      write(*,*) "S"
   end subroutine identify

   function duplicate(this) result(obj)
      class(S), intent(inout) :: this
      class(T), allocatable :: obj
      allocate(obj, source = S())
   end function duplicate

end module polymorphic_copy_example_module




program    test
   use polymorphic_copy_example_module
   implicit none

   class(T), allocatable :: Sobj
   class(T), allocatable :: Sclone

   allocate(Sobj, source = S())
   allocate(Sclone, source = Sobj % duplicate())

   call Sobj % identify()
   call Sclone % identify()

end program test
Task: Polymorphic copy







Python: import copy

class T:
   def classname(self): 
      return self.__class__.__name__

   def __init__(self):
      self.myValue = "I'm a T."

   def speak(self):
      print self.classname(), 'Hello', self.myValue

   def clone(self):
      return copy.copy(self)

class S1(T):
   def speak(self):
      print self.classname(),"Meow", self.myValue

class S2(T):
   def speak(self):
      print self.classname(),"Woof", self.myValue


print "creating initial objects of types S1, S2, and T"
a = S1()
a.myValue = 'Green'
a.speak()

b = S2()
b.myValue = 'Blue'
b.speak()

u = T()
u.myValue = 'Purple'
u.speak()

print "Making copy of a as u, colors and types should match"
u = a.clone()
u.speak()
a.speak()
print "Assigning new color to u, A's color should be unchanged."
u.myValue = "Orange"
u.speak()
a.speak()

print "Assigning u to reference same object as b, colors and types should match"
u = b
u.speak()
b.speak()
print "Assigning new color to u. Since u,b references same object b's color changes as well"
u.myValue = "Yellow"
u.speak()
b.speak()




Fortran: 


module polymorphic_copy_example_module
   implicit none
   private 
   public :: T,S

   type, abstract :: T
   contains
      procedure (T_procedure1), deferred, pass :: identify
      procedure (T_procedure2), deferred, pass :: duplicate
   end type T


   abstract interface
      subroutine T_procedure1(this)
         import  :: T
         class(T), intent(inout) :: this
      end subroutine T_procedure1
      function T_procedure2(this) result(Tobj)
         import  :: T
         class(T), intent(inout) :: this
         class(T), allocatable :: Tobj
      end function T_procedure2
   end interface

   type, extends(T) :: S
   contains
      procedure, pass :: identify
      procedure, pass :: duplicate
   end type S

 contains

   subroutine  identify(this)
      implicit none
      class(S), intent(inout) :: this
      write(*,*) "S"
   end subroutine identify

   function duplicate(this) result(obj)
      class(S), intent(inout) :: this
      class(T), allocatable :: obj
      allocate(obj, source = S())
   end function duplicate

end module polymorphic_copy_example_module




program    test
   use polymorphic_copy_example_module
   implicit none

   class(T), allocatable :: Sobj
   class(T), allocatable :: Sclone

   allocate(Sobj, source = S())
   allocate(Sclone, source = Sobj % duplicate())

   call Sobj % identify()
   call Sclone % identify()

end program test
Task: Polymorphic copy






PHP: <?php
class T {
      function name() { return "T"; }
}

class S {
      function name() { return "S"; }
}

$obj1 = new T();
$obj2 = new S();
$obj3 = clone $obj1;
$obj4 = clone $obj2;
echo $obj3->name(), "\n"; // prints "T"
echo $obj4->name(), "\n"; // prints "S"
?>





Fortran: 


module polymorphic_copy_example_module
   implicit none
   private 
   public :: T,S

   type, abstract :: T
   contains
      procedure (T_procedure1), deferred, pass :: identify
      procedure (T_procedure2), deferred, pass :: duplicate
   end type T


   abstract interface
      subroutine T_procedure1(this)
         import  :: T
         class(T), intent(inout) :: this
      end subroutine T_procedure1
      function T_procedure2(this) result(Tobj)
         import  :: T
         class(T), intent(inout) :: this
         class(T), allocatable :: Tobj
      end function T_procedure2
   end interface

   type, extends(T) :: S
   contains
      procedure, pass :: identify
      procedure, pass :: duplicate
   end type S

 contains

   subroutine  identify(this)
      implicit none
      class(S), intent(inout) :: this
      write(*,*) "S"
   end subroutine identify

   function duplicate(this) result(obj)
      class(S), intent(inout) :: this
      class(T), allocatable :: obj
      allocate(obj, source = S())
   end function duplicate

end module polymorphic_copy_example_module




program    test
   use polymorphic_copy_example_module
   implicit none

   class(T), allocatable :: Sobj
   class(T), allocatable :: Sclone

   allocate(Sobj, source = S())
   allocate(Sclone, source = Sobj % duplicate())

   call Sobj % identify()
   call Sclone % identify()

end program test