diff --git a/sandbox/ConsoleApp/Program.cs b/sandbox/ConsoleApp/Program.cs index 5b291fe..fdbe057 100644 --- a/sandbox/ConsoleApp/Program.cs +++ b/sandbox/ConsoleApp/Program.cs @@ -19,7 +19,10 @@ Console.WriteLine(json); - +[UnitOf(typeof(ulong), UnitGenerateOptions.ArithmeticOperator)] +public readonly partial struct Money +{ +} [UnitOf(typeof(int))] diff --git a/src/UnitGenerator/SourceGenerator.cs b/src/UnitGenerator/SourceGenerator.cs index 53f92e6..06d47ac 100644 --- a/src/UnitGenerator/SourceGenerator.cs +++ b/src/UnitGenerator/SourceGenerator.cs @@ -317,10 +317,12 @@ readonly partial struct {{unitTypeName}} if (prop.HasArithmeticOperator(UnitArithmeticOperators.Addition)) { net7Interfaces.Add($"IAdditionOperators<{unitTypeName}, {unitTypeName}, {unitTypeName}>"); + net7Interfaces.Add($"IUnaryPlusOperators<{unitTypeName}, {unitTypeName}>"); } if (prop.HasArithmeticOperator(UnitArithmeticOperators.Subtraction)) { net7Interfaces.Add($"ISubtractionOperators<{unitTypeName}, {unitTypeName}, {unitTypeName}>"); + net7Interfaces.Add($"IUnaryNegationOperators<{unitTypeName}, {unitTypeName}>"); } if (prop.HasArithmeticOperator(UnitArithmeticOperators.Multiply)) { @@ -330,12 +332,6 @@ readonly partial struct {{unitTypeName}} { net7Interfaces.Add($"IDivisionOperators<{unitTypeName}, {unitTypeName}, {unitTypeName}>"); } - if (prop.HasArithmeticOperator(UnitArithmeticOperators.Multiply) || - prop.HasArithmeticOperator(UnitArithmeticOperators.Division)) - { - net7Interfaces.Add($"IUnaryPlusOperators<{unitTypeName}, {unitTypeName}>"); - net7Interfaces.Add($"IUnaryNegationOperators<{unitTypeName}, {unitTypeName}>"); - } if (prop.HasArithmeticOperator(UnitArithmeticOperators.Increment)) { net7Interfaces.Add($"IIncrementOperators<{unitTypeName}>"); @@ -773,6 +769,9 @@ public static bool TryParse(ReadOnlySpan utf8Text, IFormatProvider? provid } } +"""); + sb.AppendLine($$""" + public static {{unitTypeName}} operator +({{unitTypeName}} value) => new(({{innerTypeName}})(+value.value)); """); } if (prop.HasArithmeticOperator(UnitArithmeticOperators.Subtraction)) @@ -786,17 +785,22 @@ public static bool TryParse(ReadOnlySpan utf8Text, IFormatProvider? provid } } +"""); + string zero = ""; + if (innerTypeName == "ulong") + { + zero = "0UL "; + } + + sb.AppendLine($$""" + public static {{unitTypeName}} operator -({{unitTypeName}} value) => new(({{innerTypeName}})({{zero}}-value.value)); + """); } if (prop.HasArithmeticOperator(UnitArithmeticOperators.Multiply) || prop.HasArithmeticOperator(UnitArithmeticOperators.Division)) { - sb.AppendLine($$""" - public static {{unitTypeName}} operator +({{unitTypeName}} value) => new(({{innerTypeName}})(+value.value)); - public static {{unitTypeName}} operator -({{unitTypeName}} value) => new(({{innerTypeName}})(-value.value)); - -"""); if (prop.HasArithmeticOperator(UnitArithmeticOperators.Multiply)) { sb.AppendLine($$"""