Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C#] Support for init properties on generated classes #1154

Open
enioluwas opened this issue Jun 23, 2022 · 1 comment
Open

[C#] Support for init properties on generated classes #1154

enioluwas opened this issue Jun 23, 2022 · 1 comment

Comments

@enioluwas
Copy link
Contributor

C# 9 introduced init properties that allow using object initializers to set readonly properties. This is quite useful for enforcing the copy-on-write pattern for immutable objects:

class Person
{
    public string FirstName { get; init; }
    public string LastName { get; init; }

    public Person(Person copy)
    {
        this.FirstName = copy.FirstName;
        this.LasName = copy.LastName;
    }    
}

var johnDoe = new Person { FirstName = "John", LastName = "Doe" };
var janeDoe = new Person(johnDoe) { FirstName = "Jane" };

I see the Bond compiler CLI already has a --readonly-properties switch for making private set properties on generated classes, but that is too restrictive for this use case. Is it possible to add a new switch such as --init-properties to allow the above code sample on a Bond-generated class?

Related: #439

@chwarr
Copy link
Member

chwarr commented Jun 24, 2022

Yes, this would be a welcome contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants