From 753c7d4bb148c826c0f5e5bef838837854139f26 Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Wed, 17 Mar 2021 23:55:54 +0100 Subject: [PATCH] Fix "TrySingle" signature with honest NRT annotations --- MoreLinq/Experimental/TrySingle.cs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/MoreLinq/Experimental/TrySingle.cs b/MoreLinq/Experimental/TrySingle.cs index 6be3f8212..e72bfbc82 100644 --- a/MoreLinq/Experimental/TrySingle.cs +++ b/MoreLinq/Experimental/TrySingle.cs @@ -51,7 +51,7 @@ partial class ExperimentalEnumerable /// than two elements from the sequence. /// - public static (TCardinality Cardinality, T Value) + public static (TCardinality Cardinality, T? Value) TrySingle(this IEnumerable source, TCardinality zero, TCardinality one, TCardinality many) => TrySingle(source, zero, one, many, ValueTuple.Create); @@ -96,11 +96,7 @@ public static (TCardinality Cardinality, T Value) public static TResult TrySingle(this IEnumerable source, TCardinality zero, TCardinality one, TCardinality many, - // TODO review second argument of resultSelector - // ...that can be defaulted to null for nullable references - // so the signature is not quite accurate, but can we do - // something about that? - Func resultSelector) + Func resultSelector) { if (source == null) throw new ArgumentNullException(nameof(source)); if (resultSelector == null) throw new ArgumentNullException(nameof(resultSelector)); @@ -108,7 +104,7 @@ public static TResult TrySingle(this IEnumerable so switch (source.TryGetCollectionCount()) { case 0: - return resultSelector(zero, default!); + return resultSelector(zero, default); case 1: { var item = source switch @@ -120,15 +116,15 @@ public static TResult TrySingle(this IEnumerable so return resultSelector(one, item); } case {}: - return resultSelector(many, default!); + return resultSelector(many, default); default: { using var e = source.GetEnumerator(); if (!e.MoveNext()) - return resultSelector(zero, default!); + return resultSelector(zero, default); var current = e.Current; return !e.MoveNext() ? resultSelector(one, current) - : resultSelector(many, default!); + : resultSelector(many, default); } } }