Skip to content

Commit 1d644f8

Browse files
Muximizeangularsen
andauthored
💥Remove decimal support (#1359)
### Changes - Remove `QuantityValue`, replaced with `double` - Remove `TValueType` from interfaces - Remove `IQuantity<TUnitType, out TValueType>` - Remove `IValueQuantity<out TValueType>` - Change `IQuantity<TSelf, TUnitType, out TValueType>` to `IQuantity<TSelf, TUnitType>` - Change `IArithmeticQuantity<TSelf, TUnitType, TValueType>` to `IArithmeticQuantity<TSelf, TUnitType>` ### Changes to UnitsNet.Serialiation.JsonNet - Deserializing previously serialized JSON for decimal quantities `Information`, `BitRate` and `Power` still work, but it now reads just `double Value` property and ignores `string ValueString` and `string ValueType` properties. This may lose precision compared to preserving the full `decimal` value, but `decimal` is no longer supported in v6. ### Background In #1195 @angularsen says: > If we change all 3 quantities to double, we have the potential to clean up a LOT of QuantityValue complexity. This made me wonder how deep that complexity goes so I decided to experiment, and this is the result. I must say some of these changes make me a bit sad. A lot of work and some very clever thinking went into supporting multiple numerical types, and I want to acknowledge that. 🙇 Also, I took it as far as possible but that might not be the best outcome, for example we might want to keep deserialization support. This just demonstrates a possible direction we could go in. --------- Co-authored-by: Andreas Gullberg Larsen <[email protected]>
1 parent aeb0cd7 commit 1d644f8

File tree

280 files changed

+3761
-8973
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

280 files changed

+3761
-8973
lines changed

CodeGen/Generators/NanoFrameworkGen/QuantityGenerator.cs

+11-26
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public struct {_quantity.Name}
4343
/// <summary>
4444
/// The numeric value this quantity was constructed with.
4545
/// </summary>
46-
private readonly {_quantity.ValueType} _value;
46+
private readonly double _value;
4747
4848
/// <summary>
4949
/// The unit this quantity was constructed with.
@@ -53,7 +53,7 @@ public struct {_quantity.Name}
5353
/// <summary>
5454
/// The numeric value this quantity was constructed with.
5555
/// </summary>
56-
public {_quantity.ValueType} Value => _value;
56+
public double Value => _value;
5757
5858
/// <inheritdoc />
5959
public {_unitEnumName} Unit => _unit;
@@ -66,7 +66,7 @@ public struct {_quantity.Name}
6666
/// <param name=""value"">The numeric value to construct this quantity with.</param>
6767
/// <param name=""unit"">The unit representation to construct this quantity with.</param>
6868
/// <exception cref=""ArgumentException"">If value is NaN or Infinity.</exception>
69-
public {_quantity.Name}({_quantity.ValueType} value, {_unitEnumName} unit)
69+
public {_quantity.Name}(double value, {_unitEnumName} unit)
7070
{{
7171
_value = value;
7272
_unit = unit;
@@ -79,29 +79,14 @@ public struct {_quantity.Name}
7979
8080
/// <summary>
8181
/// Represents the largest possible value of {_quantity.Name}.
82-
/// </summary>");
83-
84-
// Non decimal
85-
Writer.WLCondition(_quantity.ValueType != "decimal", $@"
86-
public static {_quantity.Name} MaxValue {{ get; }} = new {_quantity.Name}({_quantity.ValueType}.MaxValue, BaseUnit);
87-
88-
/// <summary>
89-
/// Represents the smallest possible value of {_quantity.Name}.
9082
/// </summary>
91-
public static {_quantity.Name} MinValue {{ get; }} = new {_quantity.Name}({_quantity.ValueType}.MinValue, BaseUnit);
92-
");
93-
94-
// Decimal MaxValue = 79228162514264337593543950335M
95-
Writer.WLCondition(_quantity.ValueType == "decimal", $@"
96-
public static {_quantity.Name} MaxValue {{ get; }} = new {_quantity.Name}(79228162514264337593543950335M, BaseUnit);
83+
public static {_quantity.Name} MaxValue {{ get; }} = new {_quantity.Name}(double.MaxValue, BaseUnit);
9784
9885
/// <summary>
9986
/// Represents the smallest possible value of {_quantity.Name}.
10087
/// </summary>
101-
public static {_quantity.Name} MinValue {{ get; }} = new {_quantity.Name}(-79228162514264337593543950335M, BaseUnit);
102-
");
88+
public static {_quantity.Name} MinValue {{ get; }} = new {_quantity.Name}(double.MinValue, BaseUnit);
10389
104-
Writer.WL($@"
10590
/// <summary>
10691
/// Gets an instance of this quantity with a value of 0 in the base unit Second.
10792
/// </summary>
@@ -134,7 +119,7 @@ private void GenerateConversionProperties()
134119
/// </summary>");
135120
Writer.WLIfText(2, GetObsoleteAttributeOrNull(unit));
136121
Writer.WL($@"
137-
public {_quantity.ValueType} {unit.PluralName} => As({_unitEnumName}.{unit.SingularName});
122+
public double {unit.PluralName} => As({_unitEnumName}.{unit.SingularName});
138123
");
139124
}
140125

@@ -161,7 +146,7 @@ private void GenerateStaticFactoryMethods()
161146
/// <exception cref=""ArgumentException"">If value is NaN or Infinity.</exception>");
162147
Writer.WLIfText(2, GetObsoleteAttributeOrNull(unit));
163148
Writer.WL($@"
164-
public static {_quantity.Name} From{unit.PluralName}({_quantity.ValueType} {valueParamName}) => new {_quantity.Name}({valueParamName}, {_unitEnumName}.{unit.SingularName});
149+
public static {_quantity.Name} From{unit.PluralName}(double {valueParamName}) => new {_quantity.Name}({valueParamName}, {_unitEnumName}.{unit.SingularName});
165150
");
166151
}
167152

@@ -172,7 +157,7 @@ private void GenerateStaticFactoryMethods()
172157
/// <param name=""value"">Value to convert from.</param>
173158
/// <param name=""fromUnit"">Unit to convert from.</param>
174159
/// <returns>{_quantity.Name} unit value.</returns>
175-
public static {_quantity.Name} From({_quantity.ValueType} value, {_unitEnumName} fromUnit)
160+
public static {_quantity.Name} From(double value, {_unitEnumName} fromUnit)
176161
{{
177162
return new {_quantity.Name}(value, fromUnit);
178163
}}
@@ -190,7 +175,7 @@ private void GenerateConversionMethods()
190175
/// Convert to the unit representation <paramref name=""unit"" />.
191176
/// </summary>
192177
/// <returns>Value converted to the specified unit.</returns>
193-
public {_quantity.ValueType} As({_unitEnumName} unit) => GetValueAs(unit);
178+
public double As({_unitEnumName} unit) => GetValueAs(unit);
194179
195180
/// <summary>
196181
/// Converts this {_quantity.Name} to another {_quantity.Name} with the unit representation <paramref name=""unit"" />.
@@ -207,7 +192,7 @@ private void GenerateConversionMethods()
207192
/// This is typically the first step in converting from one unit to another.
208193
/// </summary>
209194
/// <returns>The value in the base unit representation.</returns>
210-
private {_quantity.ValueType} GetValueInBaseUnit()
195+
private double GetValueInBaseUnit()
211196
{{
212197
return Unit switch
213198
{{");
@@ -223,7 +208,7 @@ private void GenerateConversionMethods()
223208
}};
224209
}}
225210
226-
private {_quantity.ValueType} GetValueAs({_unitEnumName} unit)
211+
private double GetValueAs({_unitEnumName} unit)
227212
{{
228213
if (Unit == unit)
229214
return _value;

CodeGen/Generators/NanoFrameworkGenerator.cs

-14
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,6 @@ public static void Generate(string rootDir, Quantity[] quantities, QuantityNameT
9090
GenerateQuantity(quantity, Path.Combine(outputQuantities, $"{quantity.Name}.g.cs"));
9191
GenerateProject(quantity, Path.Combine(projectPath, $"{quantity.Name}.nfproj"), versions);
9292

93-
// Convert decimal based units to floats; decimals are not supported by nanoFramework
94-
if (quantity.ValueType == "decimal")
95-
{
96-
var replacements = new Dictionary<string, string>
97-
{
98-
{ "(\\d)m", "$1d" },
99-
{ "(\\d)M", "$1d" },
100-
{ " decimal ", " double " },
101-
{ "(decimal ", "(double " }
102-
};
103-
new FileInfo(Path.Combine(outputDir, "Units", $"{quantity.Name}Unit.g.cs")).EditFile(replacements);
104-
new FileInfo(Path.Combine(outputDir, "Quantities", $"{quantity.Name}.g.cs")).EditFile(replacements);
105-
}
106-
10793
Log.Information("✅ {Quantity} (nanoFramework)", quantity.Name);
10894
}
10995
Log.Information("");

CodeGen/Generators/QuantityJsonFilesParser.cs

-15
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ private static Quantity ParseQuantityFile(string jsonFileName)
4848
?? throw new UnitsNetCodeGenException($"Unable to parse quantity from JSON file: {jsonFileName}");
4949

5050
AddPrefixUnits(quantity);
51-
FixConversionFunctionsForDecimalValueTypes(quantity);
5251
OrderUnitsByName(quantity);
5352
return quantity;
5453
}
@@ -63,20 +62,6 @@ private static void OrderUnitsByName(Quantity quantity)
6362
quantity.Units = quantity.Units.OrderBy(u => u.SingularName, StringComparer.OrdinalIgnoreCase).ToArray();
6463
}
6564

66-
private static void FixConversionFunctionsForDecimalValueTypes(Quantity quantity)
67-
{
68-
foreach (Unit u in quantity.Units)
69-
// Use decimal for internal calculations if base type is not double, such as for long or int.
70-
{
71-
if (string.Equals(quantity.ValueType, "decimal", StringComparison.OrdinalIgnoreCase))
72-
{
73-
// Change any double literals like "1024d" to decimal literals "1024m"
74-
u.FromUnitToBaseFunc = u.FromUnitToBaseFunc.Replace("d", "m");
75-
u.FromBaseToUnitFunc = u.FromBaseToUnitFunc.Replace("d", "m");
76-
}
77-
}
78-
}
79-
8065
private static void AddPrefixUnits(Quantity quantity)
8166
{
8267
var unitsToAdd = new List<Unit>();

CodeGen/Generators/UnitsNetGen/NumberExtensionsGenerator.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static class NumberTo{_quantityName}Extensions
4545
continue;
4646

4747
Writer.WL(2, $@"
48-
/// <inheritdoc cref=""{_quantityName}.From{unit.PluralName}(UnitsNet.QuantityValue)"" />");
48+
/// <inheritdoc cref=""{_quantityName}.From{unit.PluralName}(double)"" />");
4949

5050
Writer.WLIfText(2, GetObsoleteAttributeOrNull(unit.ObsoleteText));
5151

0 commit comments

Comments
 (0)