I work with persistent data structures.
I will have the complex class that I need to create a new instance, but with one or more fields with different values.
struct Data {
int field1; int field2; int field3;
public:
Data withField2(int newField2) {
return { field1, newField2, field3 };
}
};
int main()
{
Data d = { 1, 2, 3 };
std::cout << d.field2 << std::endl;
Data newD = d.withField2(4);
std::cout << newD.field2 << std::endl;
}
In the worst case scenario, I can create a bunch of methods withField1(newField1Value)as described above, but there will be enough fields to make them very dirty.
In addition, I may need new values for several fields, so there may be many more.
So is there any magic way to say
Data newData = data.with(field1Name = newField1, field2Name = newField2)
or something similar?
source
share