Skip to content

C# generator drops the default on Quantity members, so e.g. gravity starts at 0 in C# but -9.81 in C++ #286

Open

Description

@matt-edmondson

What's wrong

The C# and C++ generators produce different starting values for a Quantity member that has a number default:

  • C#: CSharpCodeGenerator.DefaultLiteralFor (Schema/Generation/CSharpCodeGenerator.cs, around lines 805-829) has no arm for Quantity members. InitialiserFor therefore returns nothing, and the generated property keeps its type's zero value.
  • C++: the C++ generator does initialise these members, via Schema.Cpp/CppFileBuilder.cs around lines 291-293 and QuantityInitialiser around lines 327-345.

Reproduction

A member Body.Weight : Quantity(Mass) with default 2.5 passes validation with no issues. The generated C# is:

[SchemaDefault(2.5D)] public ktsu.Semantics.Quantities.Mass<float> Weight { get; set; }

There is no initialiser, so Weight starts at 0. The C++ output starts it at 2.5.

The repository's own sample hits this too: samples/modernised.schema.json (around lines 529-540) declares gravity : Acceleration1D with default -9.81. C# objects start with zero gravity, while C++ objects start at -9.81. Data that leaves the field unset therefore behaves differently depending on the language.

PR #227 fixed #218 for Semantic and Vector members and noted in its body: "Out of scope: a NumberDefault on a Quantity member still gets no C# initialiser." No issue has tracked that remainder until now.

Suggested fix / acceptance criteria

  • Magnitude and 1D quantities: emit = ktsu.Semantics.Quantities.<Name><T>.Create(<invariant literal>). Create(T) exists on Mass<T> and Acceleration1D<T> in ktsu.Semantics.Quantities 5.12.1 and takes the SI base unit, which matches what C++ assigns.
  • Vector quantities (e.g. Velocity3D): put the value in every component, as the C++ side does. The type exposes X/Y/Z, One and From<Unit>(x, y, z); confirm which construction route compiles.
  • Structs that travel as bytes (TravelsAsBytes) still get their parameterless constructor.
  • Extend TestSemanticVectorAndColourMembersStartAtTheirDefaults in CodeGenerationRoundTripTests to cover one magnitude quantity and one vector quantity, and assert the runtime starting value.

Activity

  1. matt-edmondson commented on Oct 17, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    Notes: C# and C++ produce different starting values for the same schema, and the repository's own sample (gravity = -9.81) shows it. Confirm the vector-quantity construction route against ktsu.Semantics.Quantities 5.12.1 before writing the initialiser.


    Generated by Claude Code

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions