From d5fb57695c9268cc1687256309db1ca69394784e Mon Sep 17 00:00:00 2001 From: Chtoucas Date: Thu, 9 Mar 2017 10:07:09 +0100 Subject: [PATCH] Use a ValueTuple instead of StateObject. --- src/Narvalo.Common/Collections/Dictionary$.cs | 3 +- src/Narvalo.Common/Internal/TryParser$.cs | 6 ++-- src/Narvalo.Finance/Bic.cs | 8 ++--- .../Applicative/StateObject`.cs | 2 ++ src/Narvalo.Futures/Applicative/Stateful.cs | 18 +++++------ src/Narvalo.Futures/Applicative/Stateful.g.cs | 2 +- src/Narvalo.Futures/Narvalo.Futures.csproj | 4 +++ src/Narvalo.Futures/packages.config | 4 +++ src/Narvalo.Fx/Applicative/Either`2.cs | 30 ++++++------------- src/Narvalo.Fx/Applicative/Fallible.cs | 5 +--- src/Narvalo.Fx/Applicative/Maybe`1.cs | 5 +--- src/Narvalo.Fx/Applicative/Outcome.cs | 8 ++--- src/Narvalo.Fx/Applicative/Outcome`1.cs | 13 ++------ src/Narvalo.Fx/Applicative/Result`2.cs | 13 ++------ src/Narvalo.Money/Currency.cs | 16 ++++------ .../Finance/Generic/CurrencyUnit.cs | 4 +-- 16 files changed, 54 insertions(+), 87 deletions(-) create mode 100644 src/Narvalo.Futures/packages.config diff --git a/src/Narvalo.Common/Collections/Dictionary$.cs b/src/Narvalo.Common/Collections/Dictionary$.cs index 47591f7b..da011c84 100644 --- a/src/Narvalo.Common/Collections/Dictionary$.cs +++ b/src/Narvalo.Common/Collections/Dictionary$.cs @@ -27,8 +27,7 @@ public static partial class DictionaryExtensions if (key == null) { return Maybe.None; } - TValue value; - return @this.TryGetValue(key, out value) ? Maybe.Of(value) : Maybe.None; + return @this.TryGetValue(key, out TValue value) ? Maybe.Of(value) : Maybe.None; } } } diff --git a/src/Narvalo.Common/Internal/TryParser$.cs b/src/Narvalo.Common/Internal/TryParser$.cs index d105e3fb..58e6d4ff 100644 --- a/src/Narvalo.Common/Internal/TryParser$.cs +++ b/src/Narvalo.Common/Internal/TryParser$.cs @@ -12,8 +12,7 @@ internal static class TryParserExtensions if (value == null) { return null; } - T result; - return @this.Invoke(value, out result) ? result : (T?)null; + return @this.Invoke(value, out T result) ? result : (T?)null; } public static Maybe MayInvoke(this TryParser @this, string value) where T : class @@ -22,8 +21,7 @@ internal static class TryParserExtensions if (value == null) { return Maybe.None; } - T result; - return @this.Invoke(value, out result) ? Maybe.Of(result) : Maybe.None; + return @this.Invoke(value, out T result) ? Maybe.Of(result) : Maybe.None; } } } diff --git a/src/Narvalo.Finance/Bic.cs b/src/Narvalo.Finance/Bic.cs index 2e45a1aa..894edf24 100644 --- a/src/Narvalo.Finance/Bic.cs +++ b/src/Narvalo.Finance/Bic.cs @@ -181,7 +181,7 @@ public static string FromBic(string value) } else { - var retval = value.Substring(StartIndex, Length); + string retval = value.Substring(StartIndex, Length); return CheckContent(retval) ? retval : null; } } @@ -203,7 +203,7 @@ private static class CountryPart public static string FromBic(string value) { Demand.True(value.Length >= StartIndex + Length); - var retval = value.Substring(StartIndex, Length); + string retval = value.Substring(StartIndex, Length); return CheckContent(retval) ? retval : null; } @@ -223,7 +223,7 @@ private static class InstitutionPart public static string FromBic(string value, BicVersion version) { - var retval = value.Substring(StartIndex, Length); + string retval = value.Substring(StartIndex, Length); return CheckContent(retval, version) ? retval : null; } @@ -247,7 +247,7 @@ private static class LocationPart public static string FromBic(string value) { Demand.True(value.Length >= StartIndex + Length); - var retval = value.Substring(StartIndex, Length); + string retval = value.Substring(StartIndex, Length); return CheckContent(retval) ? retval : null; } diff --git a/src/Narvalo.Futures/Applicative/StateObject`.cs b/src/Narvalo.Futures/Applicative/StateObject`.cs index af225c08..22e1b7db 100644 --- a/src/Narvalo.Futures/Applicative/StateObject`.cs +++ b/src/Narvalo.Futures/Applicative/StateObject`.cs @@ -6,6 +6,7 @@ namespace Narvalo.Applicative using System.Collections; using System.Collections.Generic; + [Obsolete] public static class StateObject { public static StateObject Create(T value, TState state) @@ -13,6 +14,7 @@ public static class StateObject } // To be replaced by ValueTuple when available. + [Obsolete] public partial struct StateObject : IEquatable>, IStructuralEquatable { public StateObject(T result, TState state) diff --git a/src/Narvalo.Futures/Applicative/Stateful.cs b/src/Narvalo.Futures/Applicative/Stateful.cs index 619b9d97..d5f37478 100644 --- a/src/Narvalo.Futures/Applicative/Stateful.cs +++ b/src/Narvalo.Futures/Applicative/Stateful.cs @@ -4,8 +4,8 @@ namespace Narvalo.Applicative { using System; - // Func> - public delegate StateObject Stateful(TState state); + // Func + public delegate (T result, TState state) Stateful(TState state); // Provides the core Monad methods. public static partial class Stateful @@ -15,14 +15,14 @@ public static partial class Stateful Func> binder) => state => { - StateObject obj = @this(state); + var obj = @this(state); - return binder(obj.Result).Invoke(obj.State); + return binder(obj.result).Invoke(obj.state); }; // Initialize a stateful computation from a given value. public static Stateful Of(T value) - => state => StateObject.Create(value, state); + => state => (value, state); public static Stateful Flatten(Stateful, TState> square) => square.Bind(Stubs>.Identity); @@ -32,15 +32,15 @@ public static partial class Stateful public static partial class Stateful { public static Stateful Get() - => state => StateObject.Create(state, state); + => state => (state, state); public static Stateful Put(TState newState) - => state => StateObject.Create(Unit.Default, newState); + => state => (Unit.Default, newState); public static Stateful Modify(Func func) - => state => StateObject.Create(Unit.Default, func(state)); + => state => (Unit.Default, func(state)); public static Stateful Gets(Func func) - => state => StateObject.Create(func(state), state); + => state => (func(state), state); } } diff --git a/src/Narvalo.Futures/Applicative/Stateful.g.cs b/src/Narvalo.Futures/Applicative/Stateful.g.cs index e8902379..ffeb96a3 100644 --- a/src/Narvalo.Futures/Applicative/Stateful.g.cs +++ b/src/Narvalo.Futures/Applicative/Stateful.g.cs @@ -6,7 +6,7 @@ // behavior and will be lost if the code is regenerated. // // Runtime Version: 4.0.30319.42000 -// Microsoft.VisualStudio.TextTemplating: 14.0 +// Microsoft.VisualStudio.TextTemplating: 15.0 // //------------------------------------------------------------------------------ diff --git a/src/Narvalo.Futures/Narvalo.Futures.csproj b/src/Narvalo.Futures/Narvalo.Futures.csproj index 98b35caa..013a2423 100644 --- a/src/Narvalo.Futures/Narvalo.Futures.csproj +++ b/src/Narvalo.Futures/Narvalo.Futures.csproj @@ -11,6 +11,9 @@ + + ..\..\packages\System.ValueTuple.4.3.0\lib\netstandard1.0\System.ValueTuple.dll + @@ -106,6 +109,7 @@ + diff --git a/src/Narvalo.Futures/packages.config b/src/Narvalo.Futures/packages.config new file mode 100644 index 00000000..8c56e0c5 --- /dev/null +++ b/src/Narvalo.Futures/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/src/Narvalo.Fx/Applicative/Either`2.cs b/src/Narvalo.Fx/Applicative/Either`2.cs index a3b3936c..34d6d046 100644 --- a/src/Narvalo.Fx/Applicative/Either`2.cs +++ b/src/Narvalo.Fx/Applicative/Either`2.cs @@ -52,16 +52,13 @@ public abstract partial class Either : IStructuralEquatable, Inte [DebuggerTypeProxy(typeof(Either<,>.Left_.DebugView))] private sealed partial class Left_ : Either, IEquatable { - public Left_(TLeft value) - { - Left = value; - } + public Left_(TLeft value) => Left = value; public override bool IsLeft => true; internal override TLeft Left { get; } - internal override TRight Right { get { throw new InvalidOperationException("XXX"); } } + internal override TRight Right => throw new InvalidOperationException("XXX"); public override Maybe LeftOrNone() => Maybe.Of(Left); @@ -97,10 +94,7 @@ private sealed class DebugView { private readonly Either _inner; - public DebugView(Either inner) - { - _inner = inner; - } + public DebugView(Either inner) => _inner = inner; public TLeft Left => _inner.Left; } @@ -112,14 +106,11 @@ public DebugView(Either inner) [DebuggerTypeProxy(typeof(Either<,>.Right_.DebugView))] private sealed partial class Right_ : Either, IEquatable { - public Right_(TRight value) - { - Right = value; - } + public Right_(TRight value) => Right = value; public override bool IsLeft => false; - internal override TLeft Left { get { throw new InvalidOperationException("XXX"); } } + internal override TLeft Left => throw new InvalidOperationException("XXX"); internal override TRight Right { get; } @@ -127,7 +118,7 @@ public Right_(TRight value) public override Maybe RightOrNone() => Maybe.Of(Right); - public override Either Swap() { throw new InvalidOperationException("XXX"); } + public override Either Swap() => throw new InvalidOperationException("XXX"); public override Either SwapUnchecked() => Either.OfLeft(Right); @@ -157,10 +148,7 @@ private sealed class DebugView { private readonly Either _inner; - public DebugView(Either inner) - { - _inner = inner; - } + public DebugView(Either inner) => _inner = inner; public TRight Right => _inner.Right; } @@ -188,12 +176,12 @@ private partial class Left_ { public override TLeft ToLeft() => Left; - public override TRight ToRight() { throw new InvalidCastException("XXX"); } + public override TRight ToRight() => throw new InvalidCastException("XXX"); } private partial class Right_ { - public override TLeft ToLeft() { throw new InvalidCastException("XXX"); } + public override TLeft ToLeft() => throw new InvalidCastException("XXX"); public override TRight ToRight() => Right; } diff --git a/src/Narvalo.Fx/Applicative/Fallible.cs b/src/Narvalo.Fx/Applicative/Fallible.cs index 4b2aafb7..5e0af48b 100644 --- a/src/Narvalo.Fx/Applicative/Fallible.cs +++ b/src/Narvalo.Fx/Applicative/Fallible.cs @@ -65,10 +65,7 @@ private sealed class DebugView { private readonly Fallible _inner; - public DebugView(Fallible inner) - { - _inner = inner; - } + public DebugView(Fallible inner) => _inner = inner; public bool IsSuccess => _inner.IsSuccess; diff --git a/src/Narvalo.Fx/Applicative/Maybe`1.cs b/src/Narvalo.Fx/Applicative/Maybe`1.cs index 9ad89f8c..f882900d 100644 --- a/src/Narvalo.Fx/Applicative/Maybe`1.cs +++ b/src/Narvalo.Fx/Applicative/Maybe`1.cs @@ -115,10 +115,7 @@ private sealed class DebugView { private readonly Maybe _inner; - public DebugView(Maybe inner) - { - _inner = inner; - } + public DebugView(Maybe inner) => _inner = inner; public bool IsSome => _inner.IsSome; diff --git a/src/Narvalo.Fx/Applicative/Outcome.cs b/src/Narvalo.Fx/Applicative/Outcome.cs index 3a48eb78..ecdfcf4d 100644 --- a/src/Narvalo.Fx/Applicative/Outcome.cs +++ b/src/Narvalo.Fx/Applicative/Outcome.cs @@ -49,10 +49,7 @@ private sealed class DebugView { private readonly Outcome _inner; - public DebugView(Outcome inner) - { - _inner = inner; - } + public DebugView(Outcome inner) => _inner = inner; public bool IsSuccess => _inner.IsSuccess; @@ -146,8 +143,7 @@ public bool Equals(Outcome other) return other.IsError && Error == other.Error; } - public override bool Equals(object obj) - => (obj is Outcome) && Equals((Outcome)obj); + public override bool Equals(object obj) => (obj is Outcome) && Equals((Outcome)obj); public override int GetHashCode() => _error?.GetHashCode() ?? 0; } diff --git a/src/Narvalo.Fx/Applicative/Outcome`1.cs b/src/Narvalo.Fx/Applicative/Outcome`1.cs index afac83ed..be22e286 100644 --- a/src/Narvalo.Fx/Applicative/Outcome`1.cs +++ b/src/Narvalo.Fx/Applicative/Outcome`1.cs @@ -116,10 +116,7 @@ private sealed class DebugView { private readonly Outcome _inner; - public DebugView(Outcome inner) - { - _inner = inner; - } + public DebugView(Outcome inner) => _inner = inner; public bool IsSuccess => _inner.IsSuccess; @@ -262,14 +259,10 @@ public void OnError(Action action) #region Publicly hidden methods. bool Internal.ISecondaryContainer.Contains(string value) - { - throw new NotSupportedException(); - } + => throw new NotSupportedException(); bool Internal.ISecondaryContainer.Contains(string value, IEqualityComparer comparer) - { - throw new NotSupportedException(); - } + => throw new NotSupportedException(); // Alias for WhenSuccess(). void Internal.IContainer.When(Func predicate, Action action) diff --git a/src/Narvalo.Fx/Applicative/Result`2.cs b/src/Narvalo.Fx/Applicative/Result`2.cs index 139d135f..4839abed 100644 --- a/src/Narvalo.Fx/Applicative/Result`2.cs +++ b/src/Narvalo.Fx/Applicative/Result`2.cs @@ -113,10 +113,7 @@ private sealed class DebugView { private readonly Result _inner; - public DebugView(Result inner) - { - _inner = inner; - } + public DebugView(Result inner) => _inner = inner; public bool IsSuccess => _inner.IsSuccess; @@ -278,14 +275,10 @@ public void OnError(Action action) #region Publicly hidden methods. bool Internal.ISecondaryContainer.Contains(TError value) - { - throw new NotSupportedException(); - } + => throw new NotSupportedException(); bool Internal.ISecondaryContainer.Contains(TError value, IEqualityComparer comparer) - { - throw new NotSupportedException(); - } + => throw new NotSupportedException(); // Alias for WhenSuccess(). void Internal.IContainer.When(Func predicate, Action action) diff --git a/src/Narvalo.Money/Currency.cs b/src/Narvalo.Money/Currency.cs index db881a56..80b3119f 100644 --- a/src/Narvalo.Money/Currency.cs +++ b/src/Narvalo.Money/Currency.cs @@ -205,7 +205,7 @@ internal string MinorCurrencyCode // Added for symmetry, but use with care. public TCurrency ToCurrency() where TCurrency : Currency { - var unit = CurrencyUnit.OfType(); + TCurrency unit = CurrencyUnit.OfType(); if (Code != unit.Code) { @@ -228,8 +228,7 @@ public static Currency Of(string code) { Require.NotNull(code, nameof(code)); - short? minorUnits; - if (!Codes.TryGetValue(code, out minorUnits)) + if (!Codes.TryGetValue(code, out short? minorUnits)) { throw new CurrencyNotFoundException(Format.Current(Strings_Money.Currency_UnknownCode, code)); } @@ -240,7 +239,7 @@ public static Currency Of(string code) /// public static Currency Of(string code, CurrencyTypes types) { - var cy = TryCreate(code, types); + Currency? cy = TryCreate(code, types); if (!cy.HasValue) { throw new CurrencyNotFoundException(Format.Current(Strings_Money.Currency_UnknownCode, code)); @@ -253,8 +252,7 @@ public static Currency Of(string code, CurrencyTypes types) { Require.NotNull(code, nameof(code)); - short? minorUnits; - if (!Codes.TryGetValue(code, out minorUnits)) { return null; } + if (!Codes.TryGetValue(code, out short? minorUnits)) { return null; } return new Currency(code, minorUnits); } @@ -265,8 +263,7 @@ public static Currency Of(string code, CurrencyTypes types) if (types.Contains(CurrencyTypes.Active)) { - short? minorUnits; - if (Codes.TryGetValue(code, out minorUnits)) + if (Codes.TryGetValue(code, out short? minorUnits)) { return new Currency(code, minorUnits); } @@ -274,8 +271,7 @@ public static Currency Of(string code, CurrencyTypes types) if (types.Contains(CurrencyTypes.UserDefined)) { - short? minorUnits; - if (UserCodes.TryGetValue(code, out minorUnits)) + if (UserCodes.TryGetValue(code, out short? minorUnits)) { return new Currency(code, minorUnits); } diff --git a/src/Narvalo.Money/Finance/Generic/CurrencyUnit.cs b/src/Narvalo.Money/Finance/Generic/CurrencyUnit.cs index 32d58418..0dcb75d5 100644 --- a/src/Narvalo.Money/Finance/Generic/CurrencyUnit.cs +++ b/src/Narvalo.Money/Finance/Generic/CurrencyUnit.cs @@ -13,10 +13,10 @@ public partial class CurrencyUnit // Nevertheless, calling the constructor somehow defeats the idea of a unit being a singleton. internal static TCurrency OfType() where TCurrency : Currency { - var typeInfo = typeof(TCurrency).GetTypeInfo(); + TypeInfo typeInfo = typeof(TCurrency).GetTypeInfo(); // We expect that all built-in currency units defines this property. - var property = typeInfo.GetDeclaredProperty(SINGLETON_PROPERTY_NAME); + PropertyInfo property = typeInfo.GetDeclaredProperty(SINGLETON_PROPERTY_NAME); return property?.GetValue(null) as TCurrency; }