Skip to content

Commit a83c44d

Browse files
authored
Use pattern matching for switch statements (#1038)
1 parent 127524c commit a83c44d

File tree

116 files changed

+3770
-4340
lines changed

Some content is hidden

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

116 files changed

+3770
-4340
lines changed

CodeGen/Generators/NanoFrameworkGen/QuantityGenerator.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -208,19 +208,18 @@ private void GenerateConversionMethods()
208208
/// <returns>The value in the base unit representation.</returns>
209209
private {_quantity.BaseType} GetValueInBaseUnit()
210210
{{
211-
switch(Unit)
211+
return Unit switch
212212
{{");
213213
foreach (var unit in _quantity.Units)
214214
{
215215
var func = unit.FromUnitToBaseFunc.Replace("{x}", "_value");
216216
Writer.WL($@"
217-
case {_unitEnumName}.{unit.SingularName}: return {func};");
217+
{_unitEnumName}.{unit.SingularName} => {func},");
218218
}
219219

220220
Writer.WL($@"
221-
default:
222-
throw new NotImplementedException($""Can not convert {{Unit}} to base units."");
223-
}}
221+
_ => throw new NotImplementedException($""Can not convert {{Unit}} to base units."")
222+
}};
224223
}}
225224
226225
private {_quantity.BaseType} GetValueAs({_unitEnumName} unit)
@@ -230,19 +229,18 @@ private void GenerateConversionMethods()
230229
231230
var baseUnitValue = GetValueInBaseUnit();
232231
233-
switch(unit)
232+
return unit switch
234233
{{");
235234
foreach (var unit in _quantity.Units)
236235
{
237236
var func = unit.FromBaseToUnitFunc.Replace("{x}", "baseUnitValue");
238237
Writer.WL($@"
239-
case {_unitEnumName}.{unit.SingularName}: return {func};");
238+
{_unitEnumName}.{unit.SingularName} => {func},");
240239
}
241240

242241
Writer.WL(@"
243-
default:
244-
throw new NotImplementedException($""Can not convert {Unit} to {unit}."");
245-
}
242+
_ => throw new NotImplementedException($""Can not convert {Unit} to {unit}."")
243+
};
246244
}
247245
248246
#endregion

CodeGen/Generators/UnitsNetGen/StaticQuantityGenerator.cs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,18 @@ public static partial class Quantity
5252
[Obsolete(""QuantityType will be removed. Use FromQuantityInfo(QuantityInfo, QuantityValue) instead."")]
5353
public static IQuantity FromQuantityType(QuantityType quantityType, QuantityValue value)
5454
{
55-
switch(quantityType)
55+
return quantityType switch
5656
{");
5757
foreach (var quantity in _quantities)
5858
{
5959
var quantityName = quantity.Name;
6060
Writer.WL($@"
61-
case QuantityType.{quantityName}:
62-
return {quantityName}.From(value, {quantityName}.BaseUnit);");
61+
QuantityType.{quantityName} => {quantityName}.From(value, {quantityName}.BaseUnit),");
6362
}
6463

6564
Writer.WL(@"
66-
default:
67-
throw new ArgumentException($""{quantityType} is not a supported quantity type."");
68-
}
65+
_ => throw new ArgumentException($""{quantityType} is not a supported quantity type."")
66+
};
6967
}
7068
7169
/// <summary>
@@ -76,20 +74,18 @@ public static IQuantity FromQuantityType(QuantityType quantityType, QuantityValu
7674
/// <returns>The created quantity.</returns>
7775
public static IQuantity FromQuantityInfo(QuantityInfo quantityInfo, QuantityValue value)
7876
{
79-
switch(quantityInfo.Name)
77+
return quantityInfo.Name switch
8078
{");
8179
foreach (var quantity in _quantities)
8280
{
8381
var quantityName = quantity.Name;
8482
Writer.WL($@"
85-
case ""{quantityName}"":
86-
return {quantityName}.From(value, {quantityName}.BaseUnit);");
83+
""{quantityName}"" => {quantityName}.From(value, {quantityName}.BaseUnit),");
8784
}
8885

8986
Writer.WL(@"
90-
default:
91-
throw new ArgumentException($""{quantityInfo.Name} is not a supported quantity."");
92-
}
87+
_ => throw new ArgumentException($""{quantityInfo.Name} is not a supported quantity."")
88+
};
9389
}
9490
9591
/// <summary>
@@ -140,20 +136,18 @@ public static bool TryParse(IFormatProvider? formatProvider, Type quantityType,
140136
141137
var parser = QuantityParser.Default;
142138
143-
switch(quantityType)
139+
return quantityType switch
144140
{");
145141
foreach (var quantity in _quantities)
146142
{
147143
var quantityName = quantity.Name;
148144
Writer.WL($@"
149-
case Type _ when quantityType == typeof({quantityName}):
150-
return parser.TryParse<{quantityName}, {quantityName}Unit>(quantityString, formatProvider, {quantityName}.From, out quantity);");
145+
Type _ when quantityType == typeof({quantityName}) => parser.TryParse<{quantityName}, {quantityName}Unit>(quantityString, formatProvider, {quantityName}.From, out quantity),");
151146
}
152147

153148
Writer.WL(@"
154-
default:
155-
return false;
156-
}
149+
_ => false
150+
};
157151
}
158152
159153
internal static IEnumerable<Type> GetQuantityTypes()

UnitsNet.NanoFramework/GeneratedCode/Quantities/Acceleration.g.cs

Lines changed: 34 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.NanoFramework/GeneratedCode/Quantities/AmountOfSubstance.g.cs

Lines changed: 36 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.NanoFramework/GeneratedCode/Quantities/AmplitudeRatio.g.cs

Lines changed: 14 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)