Complex Default Values
But what if you want to set a default value for a more complex type like aCarbonImmutable object? You can use the constructor to do this:
Default Values with Validation
Even validation will work:Requirements for Complex Defaults
There are a few conditions for this approach:Use a sole property
You must always use a sole property, a property within the constructor definition won’t work
Add Optional type (recommended)
The optional type is technically not required, but it’s a good idea to use it otherwise the validation won’t work