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"
2
I think one reason why this isn’t widespread has to do with the meaning of names within a programming language. The fact that a class A
has a field x
and a class B
also has a field x
does not prove that these two fields are in any way comparable – one might be a value in a 2-D coordinate system and the other a dependent variable in a statistical modelling problem. (The standard example for this in the literature is that both a Tree
class and a Dog
class might have a member bark
, but they will probably be very different.)
As long as you deal only with A
objects, you have a certain kind of assurance that one object’s x
is like another object’s x
, only of greater or smaller value. Between classes, all bets are off. And if all the fields with similar names are comparable in the way you want, then it’s more likely that the classes should be related to begin with, so you can achieve what you want via normal Object slicing.
2
I don’t think any language would explicitly support such operation on language level. The major problem is it’s complexity. It might look simple in your example, but if you look at something like Automapper, you will notice there is tons of complexity the user of such function would want/need. And the more complex a language feature is, the more use for it there should be, so it is profitable to implement it. And while I did find few cases where such feature would be nice, it is not a common programming requirement. Also, it becomes even less useful to implement a language feature, when you can do same thing without it, see Automapper for C#.
On the other side, if you consider only map of method binding/interface instead of deep copy, then what you are looking for is called Structural typing and languages, that support it do exist, like OCaml or Go.
I know of languages with this feature, or something like it.
One that is best forgotten is COBOL’s MOVE CORRESPONDING. Look it up if you care enough.
But the more important one is the dynamic languages like Ruby and JavaScript that use dictionaries to represent structures. The kind of operations you describe are commonplace, but implemented as library functions rather than keywords in the language. Assignments are done strictly by name matching, regardless of type. They can be used (for example) in Rails to move bundles of attributes from an incoming request through program logic to database access, all by name matching.