Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using C# 7 pattern matching features #391

Merged
merged 2 commits into from
Apr 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ public static partial class Scheduler
private static T As<T>(IScheduler scheduler)
where T : class
{
var svc = scheduler as IServiceProvider;
if (svc != null)
if (scheduler is IServiceProvider svc)
{
return (T)svc.GetService(typeof(T));
}
Expand Down
9 changes: 3 additions & 6 deletions Rx.NET/Source/src/System.Reactive/Internal/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ internal static class Helpers
{
public static int? GetLength<T>(IEnumerable<T> source)
{
var array = source as T[];
if (array != null)
if (source is T[] array)
return array.Length;

var list = source as IList<T>;
if (list != null)
if (source is IList<T> list)
return list.Count;

return null;
Expand All @@ -29,8 +27,7 @@ public static IObservable<T> Unpack<T>(IObservable<T> source)
{
hasOpt = false;

var eval = source as IEvaluatableObservable<T>;
if (eval != null)
if (source is IEvaluatableObservable<T> eval)
{
source = eval.Eval();
hasOpt = true;
Expand Down
3 changes: 1 addition & 2 deletions Rx.NET/Source/src/System.Reactive/Internal/SafeObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ internal sealed class SafeObserver<TSource> : IObserver<TSource>

public static IObserver<TSource> Create(IObserver<TSource> observer, IDisposable disposable)
{
var a = observer as AnonymousObserver<TSource>;
if (a != null)
if (observer is AnonymousObserver<TSource> a)
{
return a.MakeSafe(disposable);
}
Expand Down
6 changes: 2 additions & 4 deletions Rx.NET/Source/src/System.Reactive/Linq/Observable/Catch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public _(IObserver<TSource> observer, IDisposable cancel)

protected override IEnumerable<IObservable<TSource>> Extract(IObservable<TSource> source)
{
var @catch = source as Catch<TSource>;
if (@catch != null)
if (source is Catch<TSource> @catch)
return @catch._sources;

return null;
Expand Down Expand Up @@ -129,8 +128,7 @@ public void OnNext(TSource value)

public void OnError(Exception error)
{
var e = error as TException;
if (e != null)
if (error is TException e)
{
var result = default(IObservable<TSource>);
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public _(IObserver<TSource> observer, IDisposable cancel)

protected override IEnumerable<IObservable<TSource>> Extract(IObservable<TSource> source)
{
var oern = source as OnErrorResumeNext<TSource>;
if (oern != null)
if (source is OnErrorResumeNext<TSource> oern)
return oern._sources;

return null;
Expand Down
6 changes: 2 additions & 4 deletions Rx.NET/Source/src/System.Reactive/Linq/Qbservable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,15 @@ public static IQbservable<TSource> ToQbservable<TSource>(this IQueryable<TSource

internal static Expression GetSourceExpression<TSource>(IObservable<TSource> source)
{
var q = source as IQbservable<TSource>;
if (q != null)
if (source is IQbservable<TSource> q)
return q.Expression;

return Expression.Constant(source, typeof(IObservable<TSource>));
}

internal static Expression GetSourceExpression<TSource>(IEnumerable<TSource> source)
{
var q = source as IQueryable<TSource>;
if (q != null)
if (source is IQueryable<TSource> q)
return q.Expression;

return Expression.Constant(source, typeof(IEnumerable<TSource>));
Expand Down
6 changes: 2 additions & 4 deletions Rx.NET/Source/src/System.Reactive/Linq/QbservableEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@ public static partial class QbservableEx
{
internal static Expression GetSourceExpression<TSource>(IObservable<TSource> source)
{
var q = source as IQbservable<TSource>;
if (q != null)
if (source is IQbservable<TSource> q)
return q.Expression;

return Expression.Constant(source, typeof(IObservable<TSource>));
}

internal static Expression GetSourceExpression<TSource>(IEnumerable<TSource> source)
{
var q = source as IQueryable<TSource>;
if (q != null)
if (source is IQueryable<TSource> q)
return q.Expression;

return Expression.Constant(source, typeof(IEnumerable<TSource>));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ internal partial class QueryLanguage

public virtual IObservable<TSource> AsObservable<TSource>(IObservable<TSource> source)
{
var asObservable = source as AsObservable<TSource>;
if (asObservable != null)
if (source is AsObservable<TSource> asObservable)
return asObservable;

return new AsObservable<TSource>(source);
Expand Down Expand Up @@ -48,8 +47,7 @@ private static IObservable<IList<TSource>> Buffer_<TSource>(IObservable<TSource>

public virtual IObservable<TSource> Dematerialize<TSource>(IObservable<Notification<TSource>> source)
{
var materialize = source as Materialize<TSource>;
if (materialize != null)
if (source is Materialize<TSource> materialize)
return materialize.Dematerialize();

return new Dematerialize<TSource>(source);
Expand Down Expand Up @@ -133,8 +131,7 @@ public virtual IObservable<TSource> Finally<TSource>(IObservable<TSource> source

public virtual IObservable<TSource> IgnoreElements<TSource>(IObservable<TSource> source)
{
var ignoreElements = source as IgnoreElements<TSource>;
if (ignoreElements != null)
if (source is IgnoreElements<TSource> ignoreElements)
return ignoreElements;

return new IgnoreElements<TSource>(source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,7 @@ private static IObservable<TResult> SelectMany_<TSource, TCollection, TResult>(I

public virtual IObservable<TSource> Skip<TSource>(IObservable<TSource> source, int count)
{
var skip = source as Skip<TSource>.Count;
if (skip != null)
if (source is Skip<TSource>.Count skip)
return skip.Combine(count);

return new Skip<TSource>.Count(source, count);
Expand Down Expand Up @@ -387,8 +386,7 @@ public virtual IObservable<TSource> Take<TSource>(IObservable<TSource> source, i

private static IObservable<TSource> Take_<TSource>(IObservable<TSource> source, int count)
{
var take = source as Take<TSource>.Count;
if (take != null)
if (source is Take<TSource>.Count take)
return take.Combine(count);

return new Take<TSource>.Count(source, count);
Expand All @@ -414,8 +412,7 @@ public virtual IObservable<TSource> TakeWhile<TSource>(IObservable<TSource> sour

public virtual IObservable<TSource> Where<TSource>(IObservable<TSource> source, Func<TSource, bool> predicate)
{
var where = source as Where<TSource>.Predicate;
if (where != null)
if (source is Where<TSource>.Predicate where)
return where.Combine(predicate);

return new Where<TSource>.Predicate(source, predicate);
Expand Down
12 changes: 4 additions & 8 deletions Rx.NET/Source/src/System.Reactive/Linq/QueryLanguage.Time.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,7 @@ public virtual IObservable<TSource> Skip<TSource>(IObservable<TSource> source, T

private static IObservable<TSource> Skip_<TSource>(IObservable<TSource> source, TimeSpan duration, IScheduler scheduler)
{
var skip = source as Skip<TSource>.Time;
if (skip != null && skip._scheduler == scheduler)
if (source is Skip<TSource>.Time skip && skip._scheduler == scheduler)
return skip.Combine(duration);

return new Skip<TSource>.Time(source, duration, scheduler);
Expand Down Expand Up @@ -293,8 +292,7 @@ public virtual IObservable<TSource> SkipUntil<TSource>(IObservable<TSource> sour

private static IObservable<TSource> SkipUntil_<TSource>(IObservable<TSource> source, DateTimeOffset startTime, IScheduler scheduler)
{
var skipUntil = source as SkipUntil<TSource>;
if (skipUntil != null && skipUntil._scheduler == scheduler)
if (source is SkipUntil<TSource> skipUntil && skipUntil._scheduler == scheduler)
return skipUntil.Combine(startTime);

return new SkipUntil<TSource>(source, startTime, scheduler);
Expand All @@ -316,8 +314,7 @@ public virtual IObservable<TSource> Take<TSource>(IObservable<TSource> source, T

private static IObservable<TSource> Take_<TSource>(IObservable<TSource> source, TimeSpan duration, IScheduler scheduler)
{
var take = source as Take<TSource>.Time;
if (take != null && take._scheduler == scheduler)
if (source is Take<TSource>.Time take && take._scheduler == scheduler)
return take.Combine(duration);

return new Take<TSource>.Time(source, duration, scheduler);
Expand Down Expand Up @@ -378,8 +375,7 @@ public virtual IObservable<TSource> TakeUntil<TSource>(IObservable<TSource> sour

private static IObservable<TSource> TakeUntil_<TSource>(IObservable<TSource> source, DateTimeOffset endTime, IScheduler scheduler)
{
var takeUntil = source as TakeUntil<TSource>;
if (takeUntil != null && takeUntil._scheduler == scheduler)
if (source is TakeUntil<TSource> takeUntil && takeUntil._scheduler == scheduler)
return takeUntil.Combine(endTime);

return new TakeUntil<TSource>(source, endTime, scheduler);
Expand Down
3 changes: 1 addition & 2 deletions Rx.NET/Source/src/System.Reactive/Observable.Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,7 @@ public static IDisposable SubscribeSafe<T>(this IObservable<T> source, IObserver
return source.Subscribe(observer);
}

var producer = source as IProducer<T>;
if (producer != null)
if (source is IProducer<T> producer)
{
return producer.SubscribeRaw(observer, enableSafeguard: false);
}
Expand Down
6 changes: 2 additions & 4 deletions Rx.NET/Source/src/System.Reactive/ObservableQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ public IDisposable Subscribe(IObserver<TSource> observer)

public override string ToString()
{
var c = _expression as ConstantExpression;
if (c != null && c.Value == this)
if (_expression is ConstantExpression c && c.Value == this)
{
if (_source != null)
return _source.ToString();
Expand All @@ -159,8 +158,7 @@ class ObservableRewriter : ExpressionVisitor
{
protected override Expression VisitConstant(ConstantExpression/*!*/ node)
{
var query = node.Value as ObservableQuery;
if (query != null)
if (node.Value is ObservableQuery query)
{
var source = query.Source;
if (source != null)
Expand Down
9 changes: 3 additions & 6 deletions Rx.NET/Source/src/System.Reactive/Subjects/Subject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ public override IDisposable Subscribe(IObserver<T> observer)
return Disposable.Empty;
}

var done = oldObserver as DoneObserver<T>;
if (done != null)
if (oldObserver is DoneObserver<T> done)
{
observer.OnError(done.Exception);
return Disposable.Empty;
Expand All @@ -154,8 +153,7 @@ public override IDisposable Subscribe(IObserver<T> observer)
}
else
{
var obs = oldObserver as Observer<T>;
if (obs != null)
if (oldObserver is Observer<T> obs)
{
newObserver = obs.Add(observer);
}
Expand Down Expand Up @@ -203,8 +201,7 @@ private void Unsubscribe(IObserver<T> observer)
if (oldObserver == DisposedObserver<T>.Instance || oldObserver is DoneObserver<T>)
return;

var obs = oldObserver as Observer<T>;
if (obs != null)
if (oldObserver is Observer<T> obs)
{
newObserver = obs.Remove(observer);
}
Expand Down