CollinAlpert / Lombok.NET

.NET adaptation for Java's Lombok using Source Generators.
MIT License
309 stars 16 forks source link

Field initialized readonly fields are added as Constructor Parameter with RequiredArgsConstructor Attribute #16

Closed Aytackydln closed 1 year ago

Aytackydln commented 1 year ago

This is also the behaviour in Java's Lombok. Initialized members also will be overridden because of this

CollinAlpert commented 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.

Aytackydln commented 1 year ago

But then RequiredArgsConstructor becomes the same behaviour as AllArgsConstructor?

CollinAlpert commented 1 year ago

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.

Aytackydln commented 1 year ago

Is there any case where their behaviour is different?

CollinAlpert commented 1 year ago

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.