Skip to content

Commit

Permalink
Use pattern matching for switch statements (#1038)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmilnthorp authored Feb 9, 2022
1 parent 127524c commit a83c44d
Show file tree
Hide file tree
Showing 116 changed files with 3,770 additions and 4,340 deletions.
18 changes: 8 additions & 10 deletions CodeGen/Generators/NanoFrameworkGen/QuantityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,19 +208,18 @@ private void GenerateConversionMethods()
/// <returns>The value in the base unit representation.</returns>
private {_quantity.BaseType} GetValueInBaseUnit()
{{
switch(Unit)
return Unit switch
{{");
foreach (var unit in _quantity.Units)
{
var func = unit.FromUnitToBaseFunc.Replace("{x}", "_value");
Writer.WL($@"
case {_unitEnumName}.{unit.SingularName}: return {func};");
{_unitEnumName}.{unit.SingularName} => {func},");
}

Writer.WL($@"
default:
throw new NotImplementedException($""Can not convert {{Unit}} to base units."");
}}
_ => throw new NotImplementedException($""Can not convert {{Unit}} to base units."")
}};
}}
private {_quantity.BaseType} GetValueAs({_unitEnumName} unit)
Expand All @@ -230,19 +229,18 @@ private void GenerateConversionMethods()
var baseUnitValue = GetValueInBaseUnit();
switch(unit)
return unit switch
{{");
foreach (var unit in _quantity.Units)
{
var func = unit.FromBaseToUnitFunc.Replace("{x}", "baseUnitValue");
Writer.WL($@"
case {_unitEnumName}.{unit.SingularName}: return {func};");
{_unitEnumName}.{unit.SingularName} => {func},");
}

Writer.WL(@"
default:
throw new NotImplementedException($""Can not convert {Unit} to {unit}."");
}
_ => throw new NotImplementedException($""Can not convert {Unit} to {unit}."")
};
}
#endregion
Expand Down
30 changes: 12 additions & 18 deletions CodeGen/Generators/UnitsNetGen/StaticQuantityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,18 @@ public static partial class Quantity
[Obsolete(""QuantityType will be removed. Use FromQuantityInfo(QuantityInfo, QuantityValue) instead."")]
public static IQuantity FromQuantityType(QuantityType quantityType, QuantityValue value)
{
switch(quantityType)
return quantityType switch
{");
foreach (var quantity in _quantities)
{
var quantityName = quantity.Name;
Writer.WL($@"
case QuantityType.{quantityName}:
return {quantityName}.From(value, {quantityName}.BaseUnit);");
QuantityType.{quantityName} => {quantityName}.From(value, {quantityName}.BaseUnit),");
}

Writer.WL(@"
default:
throw new ArgumentException($""{quantityType} is not a supported quantity type."");
}
_ => throw new ArgumentException($""{quantityType} is not a supported quantity type."")
};
}
/// <summary>
Expand All @@ -76,20 +74,18 @@ public static IQuantity FromQuantityType(QuantityType quantityType, QuantityValu
/// <returns>The created quantity.</returns>
public static IQuantity FromQuantityInfo(QuantityInfo quantityInfo, QuantityValue value)
{
switch(quantityInfo.Name)
return quantityInfo.Name switch
{");
foreach (var quantity in _quantities)
{
var quantityName = quantity.Name;
Writer.WL($@"
case ""{quantityName}"":
return {quantityName}.From(value, {quantityName}.BaseUnit);");
""{quantityName}"" => {quantityName}.From(value, {quantityName}.BaseUnit),");
}

Writer.WL(@"
default:
throw new ArgumentException($""{quantityInfo.Name} is not a supported quantity."");
}
_ => throw new ArgumentException($""{quantityInfo.Name} is not a supported quantity."")
};
}
/// <summary>
Expand Down Expand Up @@ -140,20 +136,18 @@ public static bool TryParse(IFormatProvider? formatProvider, Type quantityType,
var parser = QuantityParser.Default;
switch(quantityType)
return quantityType switch
{");
foreach (var quantity in _quantities)
{
var quantityName = quantity.Name;
Writer.WL($@"
case Type _ when quantityType == typeof({quantityName}):
return parser.TryParse<{quantityName}, {quantityName}Unit>(quantityString, formatProvider, {quantityName}.From, out quantity);");
Type _ when quantityType == typeof({quantityName}) => parser.TryParse<{quantityName}, {quantityName}Unit>(quantityString, formatProvider, {quantityName}.From, out quantity),");
}

Writer.WL(@"
default:
return false;
}
_ => false
};
}
internal static IEnumerable<Type> GetQuantityTypes()
Expand Down
70 changes: 34 additions & 36 deletions UnitsNet.NanoFramework/GeneratedCode/Quantities/Acceleration.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 14 additions & 16 deletions UnitsNet.NanoFramework/GeneratedCode/Quantities/AmplitudeRatio.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a83c44d

Please sign in to comment.