From 8667ae59ce942110dea2408db0032cec64d695c8 Mon Sep 17 00:00:00 2001 From: JakenVeina Date: Fri, 16 Aug 2024 14:39:43 -0500 Subject: [PATCH] Fixed a bogus use of the `default` keyword, within a ternary expression where implicit casting of a generic is in play. The expression compiles differently depending on whether the generic type in question is a value type, versus a reference type, and generates an excpetion in the case of a value type. --- .../Cache/TransformImmutableFixture.cs | 20 +++++++++++++++++++ .../Cache/Internal/TransformImmutable.cs | 4 +++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/DynamicData.Tests/Cache/TransformImmutableFixture.cs b/src/DynamicData.Tests/Cache/TransformImmutableFixture.cs index 0ac977e61..4b529b345 100644 --- a/src/DynamicData.Tests/Cache/TransformImmutableFixture.cs +++ b/src/DynamicData.Tests/Cache/TransformImmutableFixture.cs @@ -267,6 +267,26 @@ public void TransformFactoryThrows_ExceptionIsCaptured() results.IsCompleted.Should().BeFalse(); } + // /~https://github.com/reactivemarbles/DynamicData/issues/925 + [Fact] + public void TDestinationIsValueType_DoesNotThrowException() + { + using var source = new Subject>(); + + using var results = source + .TransformImmutable(transformFactory: static value => value.Length) + .AsAggregator(); + + + source.OnNext(new ChangeSet() + { + new(reason: ChangeReason.Add, key: "Item #1", current: "Item #1", index: 0) + }); + + results.Error.Should().BeNull(); + results.Messages.Count.Should().Be(1, "1 source operation was performed"); + } + private class Item { public static readonly Func IdSelector diff --git a/src/DynamicData/Cache/Internal/TransformImmutable.cs b/src/DynamicData/Cache/Internal/TransformImmutable.cs index 8e866db72..885270206 100644 --- a/src/DynamicData/Cache/Internal/TransformImmutable.cs +++ b/src/DynamicData/Cache/Internal/TransformImmutable.cs @@ -5,6 +5,8 @@ using System.Reactive; using System.Reactive.Linq; +using DynamicData.Kernel; + namespace DynamicData.Cache.Internal; internal sealed class TransformImmutable @@ -40,7 +42,7 @@ public IObservable> Run() current: _transformFactory.Invoke(change.Current), previous: change.Previous.HasValue ? _transformFactory.Invoke(change.Previous.Value) - : default, + : Optional.None(), currentIndex: change.CurrentIndex, previousIndex: change.PreviousIndex)); }