Give semantic, vector and colour members their default in generated C# [patch] - #227
Merged
Merged
Conversation
…# [patch] DefaultInitialiserFor only had arms for Int, Long, Float, Double, Bool, String and Enum, so a NumberDefault on a Semantic, Vector2/3/4 or colour member - all of which validation accepts - was written to the SchemaDefault attribute but got no initialiser, and a new instance started at zero where the C++ output started at the default. Build the default as a typed expression: a semantic type through its explicit conversion from the representation's literal, and a vector or colour with the number in every component, matching the C++ generator. A TravelsAsBytes struct gets its parameterless constructor as before, since that keys off the initialiser being non-empty. Fixes #218 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011PMSp3sjbza8Td6fxS2Kjr
|
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.



Fixes #218
What was wrong
CSharpCodeGenerator.DefaultInitialiserForonly had arms forInt,Long,Float,Double,Bool,StringandEnum. Validation also accepts aNumberDefaulton aSemanticmember (reading through to its representation) and onVector2/3/4and the colours. For those members theSchemaDefaultattribute was written but no initialiser was, sonew Body().Masscame out as0in C# while the C++ output started it at2.5. The round-trip tests only read the attribute back, so they didn't catch it.Change
DefaultInitialiserFornow delegates toDefaultLiteralFor(value, type), which builds the default as an expression of the member's type:(Kilograms)(2.5f). The inner value is parenthesised so a negative default doesn't parse as subtraction. A refinement chain such asHeavy : Kilograms : Floatworks becauseRepresentation()already walks it. A chain that never reaches a real type falls through, as validation already reports that case.new System.Numerics.Vector3(1f, 1f, 1f),new ktsu.Schema.Runtime.Vector2<int>(2, 2),new ktsu.Schema.Runtime.ColorRgba(0.5f, …).DefaultInitialiserForwraps in= …;.TravelsAsBytesstructs: they still get their parameterless constructor, because that check keys offInitialiserFor(member)being non-empty, and the new arms are included in that.Out of scope: a
NumberDefaulton aQuantitymember still gets no C# initialiser. Constructing aktsu.Semantics.Quantitiesvalue is a separate question from this issue.Tests
CodeGenerationRoundTripTests.TestSemanticVectorAndColourMembersStartAtTheirDefaultsruns once for a class and once for aTravelsAsBytesstruct. It compiles a class with these defaulted members:Float(2.5 and −1.5)IntHeavy : Kilograms)Vector3Vector2ColorRGBAIt then constructs an instance and checks each member's runtime value, not just the attribute.
With the
CSharpCodeGenerator.cschange reverted, both rows fail withExpected values to be equal. With the change, the fullSchema.Testsuite passes on net10.0 (504/504). I couldn't run the net8.0/net9.0 legs because those runtimes aren't installed in this environment. The onlydotnet formatfindings in the changed range are IDE0001 on the two pre-existingModels.Types.String/Enumarms I moved. That qualification style is used throughout the file.This PR and #226 (for #217) both touch
CSharpCodeGenerator.cs, in different methods. TheStringarm here still callsQuote, so whichever merges second should rebase cleanly.🤖 Generated with Claude Code
https://claude.ai/code/session_011PMSp3sjbza8Td6fxS2Kjr
Generated by Claude Code