Does any programming language exist to support the explicit, property-level object copy?
For example, assume this code:
public class Student
{
public string Name { get; set; }
public string Code { get; set; }
public List<string> Interests { get; set; }
}
public class Item
{
public string Name { get; set; }
public string Code { get; set; }
}
I wonder if there is a compiler (or a language) out there that can indirectly cast two objects to each other, based on property-level matching and a lossy transformation algorithm?
- Create an instance of the target class
- Using reflection, find all properties of the target class
- For each property, try to find a match in the source class, using name and type
- If found, transfer the value
- Otherwise, initialize to the default value
Of course, this should be done explicitly with the full intention of the developer. For example, a C# pseudo-syntax might look like:
Student student = new Student();
student.Name = "Saeed";
student.Code = "513223";
Item item = copy student; // assume "copy" to be a keyword
// Here, item's name is "Saeed" and its code is "513223"