Skip to content

Commit 944cc78

Browse files
committed
Use IReadOnlyCollection over IEnumerable
2 parents 23befe7 + cc1bef9 commit 944cc78

4 files changed

Lines changed: 31 additions & 24 deletions

File tree

src/ImmutableObjectGraph.Generation/CodeGen+EnumerableRecursiveParentGen.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ private void ImplementRecursiveParentInterface()
115115
// this.Children;
116116
var thisDotChildren = Syntax.ThisDot(SyntaxFactory.IdentifierName(this.generator.applyToMetaType.RecursiveField.Name.ToPascalCase()));
117117

118-
// System.Collections.Generic.IEnumerable<IRecursiveType> IRecursiveParent.Children
118+
// System.Collections.Generic.IReadOnlyCollection<IRecursiveType> IRecursiveParent.Children
119119
this.innerMembers.Add(
120120
SyntaxFactory.PropertyDeclaration(
121-
Syntax.GetTypeSyntax(typeof(IEnumerable<IRecursiveType>)),
121+
Syntax.GetTypeSyntax(typeof(IReadOnlyCollection<IRecursiveType>)),
122122
nameof(IRecursiveParent.Children))
123123
.WithExplicitInterfaceSpecifier(SyntaxFactory.ExplicitInterfaceSpecifier(Syntax.GetTypeSyntax(typeof(IRecursiveParent))))
124124
.WithExpressionBody(SyntaxFactory.ArrowExpressionClause(thisDotChildren))
@@ -164,11 +164,11 @@ private void ImplementRecursiveParentInterface()
164164
SyntaxFactory.Argument(SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, parentedVar, SyntaxFactory.IdentifierName(nameof(ParentedRecursiveTypeNonGeneric.Value)))),
165165
SyntaxFactory.Argument(SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, parentedVar, SyntaxFactory.IdentifierName(nameof(ParentedRecursiveTypeNonGeneric.Parent)))))))));
166166

167-
////System.Collections.Generic.IEnumerable<<#= templateType.RecursiveType.TypeName #>> IRecursiveParent<<#= templateType.RecursiveType.TypeName #>>.Children
167+
////System.Collections.Generic.IReadOnlyCollection<<#= templateType.RecursiveType.TypeName #>> IRecursiveParent<<#= templateType.RecursiveType.TypeName #>>.Children
168168
//// => return this.Children;
169169
this.innerMembers.Add(
170170
SyntaxFactory.PropertyDeclaration(
171-
Syntax.IEnumerableOf(this.generator.applyToMetaType.RecursiveType.TypeSyntax),
171+
Syntax.IReadOnlyCollectionOf(this.generator.applyToMetaType.RecursiveType.TypeSyntax),
172172
nameof(IRecursiveParent<IRecursiveType>.Children))
173173
.WithExplicitInterfaceSpecifier(SyntaxFactory.ExplicitInterfaceSpecifier(irecursiveParentOfT))
174174
.WithExpressionBody(SyntaxFactory.ArrowExpressionClause(thisDotChildren))
@@ -185,6 +185,15 @@ private void ImplementOrderedChildrenInterface()
185185
this.baseTypes.Add(SyntaxFactory.SimpleBaseType(Syntax.GetTypeSyntax(typeof(IRecursiveParentWithOrderedChildren))));
186186
}
187187

188+
// IReadOnlyList<IRecursiveType> IRecursiveParentWithOrderedChildren.Children => this.children;
189+
this.innerMembers.Add(SyntaxFactory.PropertyDeclaration(
190+
Syntax.IReadOnlyListOf(Syntax.GetTypeSyntax(typeof(IRecursiveType))),
191+
nameof(IRecursiveParentWithOrderedChildren.Children))
192+
.WithExplicitInterfaceSpecifier(SyntaxFactory.ExplicitInterfaceSpecifier(SyntaxFactory.IdentifierName(nameof(IRecursiveParentWithOrderedChildren))))
193+
.WithExpressionBody(SyntaxFactory.ArrowExpressionClause(Syntax.ThisDot(SyntaxFactory.IdentifierName(this.generator.applyToMetaType.RecursiveField.Name))))
194+
.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))
195+
.AddAttributeLists(SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList(DebuggerBrowsableNeverAttribute))));
196+
188197
// int IRecursiveParentWithOrderedChildren.IndexOf(IRecursiveType value)
189198
var valueParameterName = SyntaxFactory.IdentifierName("value");
190199
this.innerMembers.Add(SyntaxFactory.MethodDeclaration(

src/ImmutableObjectGraph.Generation/CodeGen+RootedStructGen.cs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -743,21 +743,17 @@ protected MethodDeclarationSyntax CreateParentedNodeMethod()
743743
protected PropertyDeclarationSyntax[] CreateChildrenProperties()
744744
{
745745
return new PropertyDeclarationSyntax[] {
746-
// IEnumerable<IRecursiveType> IRecursiveParent.Children { get; }
747-
SyntaxFactory.PropertyDeclaration(Syntax.IEnumerableOf(Syntax.GetTypeSyntax(typeof(IRecursiveType))), nameof(IRecursiveParent.Children))
746+
// IReadOnlyCollection<IRecursiveType> IRecursiveParent.Children => this.greenNode.Children;
747+
SyntaxFactory.PropertyDeclaration(Syntax.IReadOnlyCollectionOf(Syntax.GetTypeSyntax(typeof(IRecursiveType))), nameof(IRecursiveParent.Children))
748748
.WithExplicitInterfaceSpecifier(SyntaxFactory.ExplicitInterfaceSpecifier(Syntax.GetTypeSyntax(typeof(IRecursiveParent))))
749-
.AddAccessorListAccessors(SyntaxFactory.AccessorDeclaration(
750-
SyntaxKind.GetAccessorDeclaration,
751-
SyntaxFactory.Block(
752-
// return System.Linq.Enumerable.Cast<IRecursiveType>(this.Children);
753-
SyntaxFactory.ReturnStatement(
754-
Syntax.EnumerableExtension(
755-
SyntaxFactory.GenericName(nameof(Enumerable.Cast))
756-
.AddTypeArgumentListArguments(Syntax.GetTypeSyntax(typeof(IRecursiveType))),
757-
Syntax.ThisDot(this.applyTo.RecursiveParent.RecursiveField.NameAsProperty),
758-
SyntaxFactory.ArgumentList()))))),
759-
// IEnumerable<TRootedRecursiveType> IRecursiveParent<TRootedRecursiveType>.Children { get; }
760-
SyntaxFactory.PropertyDeclaration(Syntax.IEnumerableOf(this.rootedRecursiveType), nameof(IRecursiveParent<IRecursiveType>.Children))
749+
.WithExpressionBody(SyntaxFactory.ArrowExpressionClause(
750+
SyntaxFactory.MemberAccessExpression(
751+
SyntaxKind.SimpleMemberAccessExpression,
752+
Syntax.ThisDot(GreenNodeFieldName),
753+
this.applyTo.RecursiveParent.RecursiveField.NameAsProperty)))
754+
.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)),
755+
// IReadOnlyCollection<TRootedRecursiveType> IRecursiveParent<TRootedRecursiveType>.Children { get; }
756+
SyntaxFactory.PropertyDeclaration(Syntax.IReadOnlyCollectionOf(this.rootedRecursiveType), nameof(IRecursiveParent<IRecursiveType>.Children))
761757
.WithExplicitInterfaceSpecifier(SyntaxFactory.ExplicitInterfaceSpecifier(CreateIRecursiveParentOfTSyntax(this.rootedRecursiveType)))
762758
// => this.Children;
763759
.WithExpressionBody(SyntaxFactory.ArrowExpressionClause(Syntax.ThisDot(this.applyTo.RecursiveParent.RecursiveField.NameAsProperty)))

src/ImmutableObjectGraph/IRecursiveCollection.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ public interface IRecursiveType {
1313
}
1414

1515
public interface IRecursiveParent : IRecursiveType {
16-
IEnumerable<IRecursiveType> Children { get; }
16+
IReadOnlyCollection<IRecursiveType> Children { get; }
1717

1818
ParentedRecursiveType<IRecursiveParent<IRecursiveType>, IRecursiveType> GetParentedNode(IdentityFieldType identity);
1919
}
2020

2121
public interface IRecursiveParent<out TRecursiveType> : IRecursiveParent
2222
where TRecursiveType : IRecursiveType {
23-
new IEnumerable<TRecursiveType> Children { get; }
23+
new IReadOnlyCollection<TRecursiveType> Children { get; }
2424
}
2525

2626
public interface IRecursiveParentWithChildReplacement<TRecursiveType> : IRecursiveParent
@@ -30,11 +30,13 @@ public interface IRecursiveParentWithChildReplacement<TRecursiveType> : IRecursi
3030
}
3131

3232
public interface IRecursiveParentWithOrderedChildren : IRecursiveParent {
33-
int IndexOf(IRecursiveType value);
33+
new IReadOnlyList<IRecursiveType> Children { get; }
34+
35+
int IndexOf(IRecursiveType value);
3436
}
3537

3638
public interface IRecursiveParentWithSortedChildren : IRecursiveParentWithOrderedChildren {
37-
int Compare(IRecursiveType first, IRecursiveType second);
39+
int Compare(IRecursiveType first, IRecursiveType second);
3840
}
3941

4042
public interface IRecursiveParentWithLookupTable<TRecursiveType> : IRecursiveParent<TRecursiveType>

src/ImmutableObjectGraph/RecursiveTypeExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,10 @@ public static IEnumerable<TRecursiveType> GetSelfAndDescendentsBreadthFirst<TRec
292292
var visiting = nodesToVisit.Dequeue();
293293
yield return visiting;
294294

295-
var visitingAsParent = visiting as IRecursiveParent;
295+
var visitingAsParent = visiting as IRecursiveParent<TRecursiveType>;
296296
if (visitingAsParent != null && visitingAsParent.Children != null)
297297
{
298-
foreach (TRecursiveType child in visitingAsParent.Children)
298+
foreach (var child in visitingAsParent.Children)
299299
{
300300
nodesToVisit.Enqueue(child);
301301
}

0 commit comments

Comments
 (0)