Skip to content

Feature/inumber generation#917

Merged
SteveDunn merged 7 commits into
SteveDunn:mainfrom
virzak:feature/inumber-generation
Apr 24, 2026
Merged

Feature/inumber generation#917
SteveDunn merged 7 commits into
SteveDunn:mainfrom
virzak:feature/inumber-generation

Conversation

@virzak

@virzak virzak commented Apr 21, 2026

Copy link
Copy Markdown
Contributor

Resolves #910

virzak and others added 5 commits April 21, 2026 15:32
Adds a new `NumericsGeneration` enum and configuration option that lets
value objects wrapping numeric primitives opt in to implementing
`INumber<T>` and the full generic math interface hierarchy.

- New `NumericsGeneration` enum with `Omit` (default) and
  `GenerateINumberInterfaceAndMethods`
- New `GenerateCodeForINumber` source generator handles interface
  declarations and all static members (operators, properties, helpers)
- All four generators (class, struct, record class, record struct) wired up
- `NumericsGeneration` parameter added to `ValueObjectAttribute<T>`,
  `ValueObjectAttribute`, and `VogenDefaultsAttribute`; can also be set
  globally via `[assembly: VogenDefaults]`
- Snapshot tests covering double/int structs, double class, and
  custom-operator deduplication

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Replace the two Generate options (GenerateINumberInterfaceAndMethods /
GenerateINumberBaseInterfaceAndMethods) with a single Generate value that
auto-detects the richest interface the underlying type supports: INumber<T>
for signed/unsigned numerics, INumberBase<T> for types like Complex.

Fix a compile error in the generated unary negation operator for ulong-backed
value objects: C# does not allow unary minus on ulong. For all unsigned integer
types (byte, ushort, uint, ulong, nuint), mirror what the BCL's
IUnaryNegationOperators<T,T> does and emit unchecked(default(T) - value.Value)
instead of (T)(-value.Value).

Add snapshot tests for uint and ulong to cover the unsigned path.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…arameter

The three helper methods in VogenConfigurationTests were missing the new
NumericsGeneration parameter added to the VogenConfiguration record.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
snap-v8.0 generates identical INumber code to snap-v9.0.
snap-v4.8 generates plain VOs without INumber (C# 11 guard skips generation).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@virzak
virzak force-pushed the feature/inumber-generation branch from d606215 to 0f5dba3 Compare April 22, 2026 06:55
virzak and others added 2 commits April 22, 2026 02:55
- Revert Testbench to project references (local NuGet testing was transient)
- Add VOG037 warning when NumericsGeneration.Generate is set on a type
  whose underlying primitive does not implement INumberBase<T>
- Fix unary negation for byte and ushort: sub-int unsigned arithmetic
  promotes to int in C#, so cast back to primitiveType is required;
  update uint and ulong to use the same pattern for consistency
- Add snapshot tests for byte, ushort, and char (char documents the
  intentional skip when IParsable<T> is explicit-only)
- Add NumericsGenerationAnalyzerTests with 3 cases covering VOG037
  (emitted for string, not emitted for double or Complex)
- Document numericsGeneration in Configuration.md
- Fill out Analyzer-Rules.md with the full rule table including VOG037

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@virzak
virzak marked this pull request as ready for review April 22, 2026 07:17
@SteveDunn

Copy link
Copy Markdown
Owner

Excellent work, thank you very much! I'd be interested to see how you use it.

@SteveDunn
SteveDunn merged commit 3d9b9ec into SteveDunn:main Apr 24, 2026
6 checks passed
@virzak
virzak deleted the feature/inumber-generation branch April 26, 2026 02:54
@virzak

virzak commented Apr 26, 2026

Copy link
Copy Markdown
Contributor Author

Excellent work, thank you very much! I'd be interested to see how you use it.

It is used pretty much like it was described in the issue, but I'll provide a few other examples.

Do you know when the next nuget release will happen?

Thanks for merging!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

INumber<T> implementaiton

2 participants