RustyOptions 0.5.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package RustyOptions --version 0.5.0                
NuGet\Install-Package RustyOptions -Version 0.5.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RustyOptions" Version="0.5.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RustyOptions --version 0.5.0                
#r "nuget: RustyOptions, 0.5.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install RustyOptions as a Cake Addin
#addin nuget:?package=RustyOptions&version=0.5.0

// Install RustyOptions as a Cake Tool
#tool nuget:?package=RustyOptions&version=0.5.0                

RustyOptions

Option and Result types for C#, inspired by Rust

CI CodeQL codecov

Avoid Null-Reference Errors

The C# nullable reference types feature is useful, but it's entirely optional, and the warnings it produces are easily ignored.

RustyOptions uses the type system to:

  • Make it impossible to access a possibly-missing value without first checking if the value is present.
  • Clearly express your intent in the API you build. If a method might not return a value, or might return an error message instead of a value, you can express this in the return type where it can't be missed.

Safely Chain Together Fallible Methods

var output = Option.Parse<int>(userInput)
    .AndThen(ValidateRange)
    .OrElse(() => defaultsByGroup.GetValueOrNone(user.GroupId))
    .MapOr(PillWidget.Render, string.Empty);

The example above does the following:

  1. Attempts to parse the user input into an integer.
  2. If the parsing succeeds, passes the resulting number to the ValidateRange method, which returns Some(parsedInput) if the parsed input is within the valid range, or None if it falls outside the valid range.
  3. If either steps 1 or 2 fail, we attempt to do a dictionary lookup to get a default value using the current user's group ID.
  4. If at the end we have a value, we render it to a string. Otherwise, we set output to an empty string.

Uses Modern .NET Features

For performance and convenience:

  • Supports parsing any type that implements IParsable<T> or ISpanParsable<T> (.NET 7 and above only)
  • The NumericOption<T> type supports generic math for any contained type that implements INumber<T> (.NET 7 and above only)
  • Supports async and IAsyncEnumerable<T>.
  • Supports nullable type annotations.
  • Supports serialization and deserialization with System.Text.Json.
  • IEquatable<T> and IComparable<T> allow Option and Result types to be easily compared and sorted.
  • IFormattable and ISpanFormattable allow Option and Result to efficiently format their content.
  • Option and Result can be efficiently converted to ReadOnlySpan<T> or IEnumerable<T> for easier interop with existing code.
  • Convenient extension methods for working with dictionaries (GetValueOrNone), collections (FirstOrNone), enums (Option.ParseEnum) and more.
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on RustyOptions:

Package Downloads
RustyOptions.FSharp

F# support for RustyOptions

oml

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.10.1 2,756 12/28/2023
0.10.0 206 12/3/2023
0.9.0 380 4/29/2023
0.8.0 357 2/6/2023
0.7.0 382 2/1/2023
0.6.1 388 1/31/2023
0.6.0 377 1/24/2023
0.5.0 305 1/18/2023
0.4.0 338 1/17/2023
0.3.0 302 1/9/2023
0.2.0 328 1/3/2023
0.1.0 545 12/31/2022