Property-level value transformation for indirect object casting

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

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật