Construct records and immutable types through their constructor - #6
Merged
Merged
Conversation
CtorSource<T> picks the constructor once the registered members are known and feeds parameters from the members with the same name and a compatible type (ConstructorResolver, IMemberBoundFactory). The object builder hands the member sources to the factory and does not set consumed members again. Preference: most members that cannot be set otherwise, then fewest parameters, so ordinary classes keep their parameterless constructor. DefaultTypeConvention registers get-only properties that match a constructor parameter, the factory fallback uses CtorSource for concrete classes. Unmatched parameters get their declared default, the default of their value type, or a session generated object one level deeper so recursive constructor parameters honor the recursion limit; structs without bound members stay default. Get-only members without a constructor parameter throw a clear message. Nullable annotations apply to constructor arguments. Tests, README and release notes. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
CtorSource<T>now picks the constructor once the registered members are known and feeds parameters from the members with the same name (case-insensitive) and a compatible type. Records need no parameterless constructor anymore, get-only properties of immutable classes are populated through the constructor.ConstructorResolver(Util) andIMemberBoundFactory(Engine.Interfaces): theObjectBuilderhands the member sources to the factory and skips the set actions of consumed members. Preference: most members that cannot be set otherwise, then fewest parameters, then most matches. Ordinary classes keep their parameterless constructor and setters, no change for them.DefaultTypeConventionregisters get-only properties that match a constructor parameter, so member conventions apply to them.ApplyTypeFactoryActionfalls back toCtorSource<T>for concrete classes (wasDefaultSource<T>/ Activator).DateOnlyparameter no longer throws.InvalidOperationExceptionnaming the property (was a reflection error at generation time).new CtorSource<T>(constructorInfo)still pins a constructor and takes matching members. Nullable annotations apply to constructor arguments.Impose/Source(...)at generation time keep working on positional record properties.Test plan
dotnet build -c Releasezero warnings🤖 Generated with Claude Code