1

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?

  1. Create an instance of the target class
  2. Using reflection, find all properties of the target class
  3. For each property, try to find a match in the source class, using name and type
  4. If found, transfer the value
  5. 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
  • You can achieve this using reflection and probably annotations to express the mapping. I wonder why would you need this, instead of having your class explicitly implement an interface that helps return a property object you're asking for (I suspect that a Map<String, String>?)
    – 9000
    Aug 13, 2014 at 6:02
  • It's because of something I saw, about "single essence, multiple existence". Aug 13, 2014 at 6:13

3 Answers 3

4

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
  • good reasoning. I also like to add that "Dog's bark" is an action, while "Tree's bark" is a noun. Based on these differences, we might get more specific. I mentioned type comparison too. A method is not equivalent to a property (attribute). Though using semantics we can achieve more, your argument is still valid. Aug 13, 2014 at 6:49
  • Problem is Java and C# insist that everything must be distinguished by name, even for those cases where you really only care about the structure of things. C# came close with anonymous types, but you can't return them from functions.
    – Doval
    Aug 13, 2014 at 14:49
3

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.

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.