Closed Aytackydln closed 1 year ago
I don't think it would be a good idea to remove field-initialized members from the constructor. Rather add your own additional constructor which excludes them or make the fields static, then the generator won't include them.
But then RequiredArgsConstructor becomes the same behaviour as AllArgsConstructor?
Yes, indeed this can become the case in some scenarios:
public class Person
{
private string _name;
private int _age;
}
public class Person
{
private readonly string _name;
private readonly int _age;
}
In cases like these, the functionality of [AllArgsConstructor]
and [RequiredArgsConstructor]
is the same. This is intended.
Is there any case where their behaviour is different?
Absolutely, imagine something like this:
public class Person
{
private readonly int _id;
private string _name;
public void SetName(string name)
{
_name = name;
}
}
[AllArgsConstructor]
would generate a constructor for both fields, while [RequiredArgsConstructor]
would generate a constructor only containing the ID.
This is also the behaviour in Java's Lombok. Initialized members also will be overridden because of this