Skip to content

Commit 0d41a88

Browse files
authored
Merge pull request #712 from akarnokd/SimplifyDefaultExpr
4.x: simplify default(X) usages
2 parents 0d66c05 + 76a070b commit 0d41a88

File tree

5 files changed

+57
-17
lines changed

5 files changed

+57
-17
lines changed

Rx.NET/Source/src/System.Reactive/Linq/Observable/Aggregate.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ public _(Func<TSource, TSource, TSource> accumulator, IObserver<TSource> observe
2929
: base(observer)
3030
{
3131
_accumulator = accumulator;
32-
_accumulation = default;
33-
_hasAccumulation = false;
3432
}
3533

3634
public override void OnNext(TSource value)
@@ -48,11 +46,18 @@ public override void OnNext(TSource value)
4846
}
4947
catch (Exception exception)
5048
{
49+
_accumulation = default;
5150
ForwardOnError(exception);
5251
}
5352
}
5453
}
5554

55+
public override void OnError(Exception error)
56+
{
57+
_accumulation = default;
58+
ForwardOnError(error);
59+
}
60+
5661
public override void OnCompleted()
5762
{
5863
if (!_hasAccumulation)
@@ -61,7 +66,9 @@ public override void OnCompleted()
6166
}
6267
else
6368
{
64-
ForwardOnNext(_accumulation);
69+
var accumulation = _accumulation;
70+
_accumulation = default;
71+
ForwardOnNext(accumulation);
6572
ForwardOnCompleted();
6673
}
6774
}
@@ -105,18 +112,22 @@ public override void OnNext(TSource value)
105112
}
106113
catch (Exception exception)
107114
{
115+
_accumulation = default;
108116
ForwardOnError(exception);
109117
}
110118
}
111119

112120
public override void OnError(Exception error)
113121
{
122+
_accumulation = default;
114123
ForwardOnError(error);
115124
}
116125

117126
public override void OnCompleted()
118127
{
119-
ForwardOnNext(_accumulation);
128+
var accumulation = _accumulation;
129+
_accumulation = default;
130+
ForwardOnNext(accumulation);
120131
ForwardOnCompleted();
121132
}
122133
}

Rx.NET/Source/src/System.Reactive/Linq/Observable/LastAsync.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ internal sealed class _ : IdentitySink<TSource>
2727
public _(IObserver<TSource> observer)
2828
: base(observer)
2929
{
30-
_value = default;
31-
_seenValue = false;
30+
3231
}
3332

3433
public override void OnNext(TSource value)
@@ -37,6 +36,12 @@ public override void OnNext(TSource value)
3736
_seenValue = true;
3837
}
3938

39+
public override void OnError(Exception error)
40+
{
41+
_value = default;
42+
ForwardOnError(error);
43+
}
44+
4045
public override void OnCompleted()
4146
{
4247
if (!_seenValue)
@@ -45,7 +50,9 @@ public override void OnCompleted()
4550
}
4651
else
4752
{
48-
ForwardOnNext(_value);
53+
var value = _value;
54+
_value = default;
55+
ForwardOnNext(value);
4956
ForwardOnCompleted();
5057
}
5158
}
@@ -77,9 +84,6 @@ public _(Func<TSource, bool> predicate, IObserver<TSource> observer)
7784
: base(observer)
7885
{
7986
_predicate = predicate;
80-
81-
_value = default;
82-
_seenValue = false;
8387
}
8488

8589
public override void OnNext(TSource value)
@@ -92,6 +96,7 @@ public override void OnNext(TSource value)
9296
}
9397
catch (Exception ex)
9498
{
99+
_value = default;
95100
ForwardOnError(ex);
96101
return;
97102
}
@@ -103,6 +108,12 @@ public override void OnNext(TSource value)
103108
}
104109
}
105110

111+
public override void OnError(Exception error)
112+
{
113+
_value = default;
114+
ForwardOnError(error);
115+
}
116+
106117
public override void OnCompleted()
107118
{
108119
if (!_seenValue)
@@ -111,7 +122,9 @@ public override void OnCompleted()
111122
}
112123
else
113124
{
114-
ForwardOnNext(_value);
125+
var value = _value;
126+
_value = default;
127+
ForwardOnNext(value);
115128
ForwardOnCompleted();
116129
}
117130
}

Rx.NET/Source/src/System.Reactive/Linq/Observable/LastOrDefaultAsync.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,25 @@ internal sealed class _ : IdentitySink<TSource>
2626
public _(IObserver<TSource> observer)
2727
: base(observer)
2828
{
29-
_value = default;
29+
3030
}
3131

3232
public override void OnNext(TSource value)
3333
{
3434
_value = value;
3535
}
3636

37+
public override void OnError(Exception error)
38+
{
39+
_value = default;
40+
ForwardOnError(error);
41+
}
42+
3743
public override void OnCompleted()
3844
{
39-
ForwardOnNext(_value);
45+
var value = _value;
46+
_value = default;
47+
ForwardOnNext(value);
4048
ForwardOnCompleted();
4149
}
4250
}
@@ -66,8 +74,6 @@ public _(Func<TSource, bool> predicate, IObserver<TSource> observer)
6674
: base(observer)
6775
{
6876
_predicate = predicate;
69-
70-
_value = default;
7177
}
7278

7379
public override void OnNext(TSource value)
@@ -80,6 +86,7 @@ public override void OnNext(TSource value)
8086
}
8187
catch (Exception ex)
8288
{
89+
_value = default;
8390
ForwardOnError(ex);
8491
return;
8592
}
@@ -90,9 +97,17 @@ public override void OnNext(TSource value)
9097
}
9198
}
9299

100+
public override void OnError(Exception error)
101+
{
102+
_value = default;
103+
ForwardOnError(error);
104+
}
105+
93106
public override void OnCompleted()
94107
{
95-
ForwardOnNext(_value);
108+
var value = _value;
109+
_value = default;
110+
ForwardOnNext(value);
96111
ForwardOnCompleted();
97112
}
98113
}

Rx.NET/Source/tests/Tests.System.Reactive/Tests.System.Reactive.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<PropertyGroup>
33
<TargetFrameworks>net46;netcoreapp2.0</TargetFrameworks>
44
<NoWarn>$(NoWarn);CS0618</NoWarn>
5+
<LangVersion>7.1</LangVersion>
56
</PropertyGroup>
67

78
<ItemGroup>

Rx.NET/Source/tests/Tests.System.Reactive/Tests/Linq/Observable/UsingTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void Using_Null()
4646
_d = d;
4747
createInvoked++;
4848
xs = scheduler.CreateColdObservable(
49-
OnNext<long>(100, scheduler.Clock),
49+
OnNext(100, scheduler.Clock),
5050
OnCompleted<long>(200));
5151
return xs;
5252
}

0 commit comments

Comments
 (0)