Skip to content

Commit ebb6408

Browse files
authored
Merge pull request #67 from Ted-Jin-Lab/development
✨Add the sum and average methods for the Quantities.
2 parents e796074 + 08dc686 commit ebb6408

3 files changed

Lines changed: 31 additions & 15 deletions

File tree

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project>
33
<PropertyGroup>
4-
<Version>2025.11.20.4</Version>
4+
<Version>2025.11.21.0</Version>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
<LangVersion>preview</LangVersion>

Tests/TedToolkit.Console/Absorptance.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ public static void Test()
1212
var c = a / b;
1313
double d = a;
1414
AbsorbedDose e = 10.0;
15+
16+
var list = new List<AbsorbedDose> {a, b};
17+
var f = list.Sum();
1518
}
1619
}
1720

src/analyzers/TedToolkit.Quantities.Analyzer/QuantityStructGenerator.cs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -166,20 +166,6 @@ public void GenerateCode(SourceProductionContext context)
166166
CreateMathMethod(nameof(Math.Floor)),
167167
CreateMathMethod(nameof(Math.Ceiling)),
168168
CreateMathMethod(nameof(Math.Round)),
169-
CreateEnumerableMethod(nameof(Enumerable.Sum), $"""
170-
/// <summary>
171-
/// Computes the sum of a sequence of <see cref="{quantity.Name}"/> values
172-
/// </summary>
173-
/// <param name="values"></param>
174-
/// <returns>The sum of the projected values</returns>
175-
"""),
176-
CreateEnumerableMethod(nameof(Enumerable.Average), $"""
177-
/// <summary>
178-
/// Computes the average of a sequence of <see cref="{quantity.Name}"/> values that are obtained by invoking a transform function on each element of the input sequence
179-
/// </summary>
180-
/// <param name="values"></param>
181-
/// <returns>The average of the sequence of values</returns>
182-
""")
183169
]);
184170
}
185171

@@ -890,6 +876,32 @@ public void GenerateCode(SourceProductionContext context)
890876
..mathsExtensions
891877
])
892878
]);
879+
880+
if (typeName.Symbol.IsFloatingPoint())
881+
{
882+
nameSpace = nameSpace.AddMembers(
883+
ClassDeclaration(quantity.Name + "Extensions")
884+
.WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)))
885+
.WithAttributeLists([GeneratedCodeAttribute(typeof(QuantityStructGenerator))])
886+
.WithXmlComment($"/// <summary>Some extension about {quantity.Name}</summary>")
887+
.WithMembers([
888+
CreateEnumerableMethod(nameof(Enumerable.Sum), $"""
889+
/// <summary>
890+
/// Computes the sum of a sequence of <see cref="{quantity.Name}"/> values
891+
/// </summary>
892+
/// <param name="values"></param>
893+
/// <returns>The sum of the projected values</returns>
894+
"""),
895+
CreateEnumerableMethod(nameof(Enumerable.Average), $"""
896+
/// <summary>
897+
/// Computes the average of a sequence of <see cref="{quantity.Name}"/> values that are obtained by invoking a transform function on each element of the input sequence
898+
/// </summary>
899+
/// <param name="values"></param>
900+
/// <returns>The average of the sequence of values</returns>
901+
""")
902+
]));
903+
}
904+
893905
context.AddSource(quantity.Name + ".g.cs", nameSpace.NodeToString());
894906
return;
895907

@@ -917,6 +929,7 @@ MemberDeclarationSyntax CreateEnumerableMethod(string methodName, string xml)
917929
.WithParameterList(ParameterList(
918930
[
919931
Parameter(Identifier("values"))
932+
.WithModifiers(TokenList(Token(SyntaxKind.ThisKeyword)))
920933
.WithType(GenericName(Identifier("global::System.Collections.Generic.IEnumerable"))
921934
.WithTypeArgumentList(TypeArgumentList([IdentifierName(quantity.Name)])))
922935
]))

0 commit comments

Comments
 (0)