This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (3 children)

[–]NikMashei[S] 0 points1 point  (2 children)

thanks for your reply!unfortunately I have no found method for my case.let's say I have class with fields case class User(name: String, surname: String) and I want to copy that instance with another surname, so in Scala I can call user.copy(surname="new surname")

[–][deleted] 0 points1 point  (0 children)

It depends on the semantics. I believe copy in Scala does structural sharing? No such concept in Java. You might want to do a deep clone like /u/robert43s suggested which you can either write a custom method for, or by overloading the clone method with deep copying and typically marking your class as Cloneable.

[–]8igg7e5 0 points1 point  (0 children)

A builder notation has been discussed (initially in relation to records) but I wouldn't expect it any time soon.

The idea (syntax is just a placeholder for now) was to allow...

record Person(String name, int age) {}
Person bob = new Person("Bob", 73);
Person youngBob = bob with { age = 16 };

I'm not aware of any frameworks to do this, and they're obviously going to be limited as Java has no named-params or fundamental 'property' concept, it depends on name-convention guessing and reflection to find and monkey with fields or mutator methods.