You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/MrKWatkins.Ast/Children.cs
+33Lines changed: 33 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -283,6 +283,39 @@ public TChild FirstOfType<TChild>()
283
283
whereTChild:TNode=>
284
284
FirstOfTypeOrDefault<TChild>()??thrownewInvalidOperationException($"Expected {parent.GetType().SimpleName()} to have a child of type {typeof(TChild).SimpleName()} but found none.");
285
285
286
+
/// <summary>
287
+
/// Returns the last node in the collection of the specified type or a specified default if it doesn't contain any nodes of the specified type.
288
+
/// </summary>
289
+
/// <typeparam name="TChild">The type of the node to return.</typeparam>
290
+
/// <param name="default">The default value to return if the collection does not contain any nodes of type <typeparamref name="TChild"/>.</param>
291
+
/// <returns>The last node if it is of the specified type or <paramref name="default"/> if it doesn't contain any nodes of the specified type.</returns>
// Manually iterating for performance. Looks like LINQ's Reverse() doesn't optimise for IList<T>.
297
+
for(varf=Count-1;f>=0;f--)
298
+
{
299
+
if(nodes[f]isTChildchild)
300
+
{
301
+
returnchild;
302
+
}
303
+
}
304
+
305
+
return@default;
306
+
}
307
+
308
+
/// <summary>
309
+
/// Returns the last node in the collection of the specified type or throws otherwise.
310
+
/// </summary>
311
+
/// <typeparam name="TChild">The type of the node to return.</typeparam>
312
+
/// <returns>The last node if it is of the specified type.</returns>
313
+
/// <exception cref="InvalidOperationException">If the collection doesn't contain any nodes of the specified type.</exception>
314
+
[Pure]
315
+
publicTChildLastOfType<TChild>()
316
+
whereTChild:TNode=>
317
+
LastOfTypeOrDefault<TChild>()??thrownewInvalidOperationException($"Expected {parent.GetType().SimpleName()} to have a child of type {typeof(TChild).SimpleName()} but found none.");
318
+
286
319
/// <summary>
287
320
/// Returns the only node in the collection of the specified type. Returns the specified default if there are no nodes in the collection of the
288
321
/// specified type. Throws if there are multiple nodes in the collection of the specified type.
0 commit comments