-
Notifications
You must be signed in to change notification settings - Fork 15
OptionParsers
Succinc<T> offers an alternative to the standard bool/out-style TryParse methods offered by various primitive types, by returning an Option<T>
, rather than a boolean.
Each parser returns an Option<T>
, of None
if the parse fails and of the Some<T>
if it does. All of them are implemented as extension methods to the string
type.
public static Option<bool> TryParseBoolean(this string source)
var str1 = "true";
var str2 = "xxxx";
var possibleBool1 = str1.TryParseBoolean();
var possibleBool2 = str2.TryParseBoolean();
possibleBool1.HasValue // true
possibleBool1.Value // true
possibleBool2.HasValue // false
public static Option<T> TryParseEnum<T>(this string source)
public static Option<T> TryParseEnumIgnoringCase<T>(this string source)
enum Colors { Red, Green, Blue }
var str1 = "Red";
var str2 = "green";
var str3 = "Yellow";
var possibleEnum1 = str1.TryParseEnum();
var possibleEnum2 = str2.TryParseEnumIgnoringCase();
var possibleEnum3 = str3.TryParseEnum();
possibleEnum1.HasValue // true
possibleEnum1.Value // Red
possibleEnum2.HasValue // true
possibleEnum2.Value // Green
possibleEnum3.HasValue // false
NOTE. Prior to v4 of Succinc<T>, the above two methods are generically constrained using where T : struct, IConvertible
and, at runtime, any call to TryParseEnum
or TryParseEnumIgnoringCase
that doesn't use an enum
would result in an ArgumentException
being thrown. As of v4, the methods are now generically constrained using where T : struct, enum
, so this shortcoming goes away.
public static Option<double> TryParseDouble(this string source)
public static Option<float> TryParseFloat(this string source)
var possibleFloat1 = "1.1".TryParseFloat();
var possibleFloat2 = "1.1.1".TryParseFloat();
var possibleDouble1 = "5.0E-324".TryParseDouble();
var possibleDouble2 = "8.0E400".TryParseDouble();
possibleFloat1.HasValue // true
possibleFloat1.Value // 1.1
possibleFloat2.HasValue // false
possibleDouble1.HasValue // true
possibleDouble1.Value // 5.0E-324
possibleDouble2.HasValue // false
public static Option<sbyte> TryParseSignedByte(this string source)
public static Option<byte> TryParseUnsignedByte(this string source)
public static Option<short> TryParseShort(this string source)
public static Option<ushort> TryParseUnsignedShort(this string source)
public static Option<int> TryParseInt(this string source)
public static Option<uint> TryParseUnsignedInt(this string source)
public static Option<long> TryParseLong(this string source)
public static Option<ulong> TryParseUnsignedLong(this string source)
var possibleSbyte1 = str1.TryParseSignedByte(127);
var possibleSbyte2 = str2.TryParseSignedByte(128);
possibleBool1.HasValue // true
possibleBool1.Value // 127
possibleBool2.HasValue // false
var possibleByte1 = str1.TryParseUnsignedByte(255);
var possibleByte2 = str2.TryParseUnsignedByte(-1);
possibleBool1.HasValue // true
possibleBool1.Value // 255
possibleBool2.HasValue // false
...
Not all parsers are shown above for brevity. Each operates as shown above though: returning the value if a valid integer within the range of the type specified, and None
otherwise.
Action
/Func
conversionsCycle
methods- Converting between
Action
andFunc
- Extension methods for existing types that use
Option<T>
- Indexed enumerations
IEnumerable<T>
cons- Option-based parsers
- Partial function applications
- Pattern matching
- Pipe Operators
- Typed lambdas
Any
Either<TLeft,TRight>
None
Option<T>
Success<T>
Union<T1,T2>
Union<T1,T2,T3>
Union<T1,T2,T3,T4>
Unit
ValueOrError