Skip to content

Vector0 quantities do not enforce the non-negativity invariant #50

Closed

Description

@matt-edmondson

Summary

docs/strategy-unified-vector-quantities.md defines IVector0<TSelf, T> as a magnitude-only quantity that "enforces non-negativity as a type-level invariant", citing examples like:

Speed<double> s = Speed.FromMetersPerSecond(-1.0);   // throws: magnitude cannot be negative

In practice, no enforcement exists. Generated Vector0 quantities (Speed, Mass, Distance, Energy, Area, …) accept any value, including negatives.

Evidence

Semantics.Quantities/Generated/.../Mass.g.cs:

public static Mass<T> FromKilogram(T value) => Create(value);

No guard. grep -E "cannot be negative|ArgumentOutOfRange|absolute zero" Generated/.../{Speed,Mass,Temperature}.g.cs returns nothing.

Semantics.Quantities/PhysicalQuantity.cs also does not validate; it is a thin wrapper exposing Value.

Suggested next step

Decide where the constraint lives:

  1. Emit a guard inside the Create / From* factories of any quantity declared as Vector0 in dimensions.json, throwing ArgumentException (per CLAUDE.md's validation guidance) when T.Sign(value) < 0.
  2. Or move the guard into a shared helper on PhysicalQuantity<T> / a Vector0<T> base.

Either way, add tests in Semantics.Test that assert negative-input rejection across every Vector0 quantity.

Area / Severity

SourceGenerators / Quantities · incomplete

Activity

  1. added a commit that references this issue on May 9, 2026
    0ee52bd
  2. matt-edmondson commented on May 9, 2026

    @matt-edmondson
    ContributorAuthor

    Resolved by #68 (commit 039afc4). Vector0Guards.EnsureNonNegative is invoked from every generated From{Unit} factory on V0 base types and V0 semantic overloads, throwing ArgumentException for negative inputs. Coverage is in Semantics.Test/Quantities/Vector0InvariantTests.cs (Speed, Mass, Length, Energy, Distance, Weight, plus float/decimal storage variants). The committed Generated/ tree was stale relative to that work and is being refreshed in #70.


    Generated by Claude Code

  3. added a commit that references this issue on Jun 25, 2026
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