Typesafe.With 1.2.0

dotnet add package Typesafe.With --version 1.2.0
NuGet\Install-Package Typesafe.With -Version 1.2.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="Typesafe.With" Version="1.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Typesafe.With --version 1.2.0
#r "nuget: Typesafe.With, 1.2.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 Typesafe.With as a Cake Addin
#addin nuget:?package=Typesafe.With&version=1.2.0

// Install Typesafe.With as a Cake Tool
#tool nuget:?package=Typesafe.With&version=1.2.0

Build status License: MIT NuGet version

Typesafe.With

Providing with-functionality to any type on the .NET platform.

What and Why?

Typesafe.With is a strongly typed way to change properties on immutable types that lets you:

  • Avoid writing with methods by hand ever again
  • Update properties on any type

Features

  • Robust and well tested
  • Self contained with no dependencies
  • Easily installed through NuGet
  • Supports .NET Core (.NET Standard 2+)
  • Focused but full-featured API

Supports

Typesafe.With supports types using:

  • Constructors
  • Property setters
  • A mix of constructors and property setters

Installation

PM> Install-Package Typesafe.With

Concepts

Typesafe.With allows you to update properties on immutable types in a type safe way that is type checked by the compiler.

Typesafe.With provides a single method called With. Any call to With produces a new instance of the immutable type with the updated property.

The call to With is strongly typed which means that the compiler verifies that the assignment is valid.

Usage

Using the library

To use Typesafe.With simply import the following namespace:

using Typesafe.With;

Update a property

public class Person
{
    public string Name { get; }
    
    public Person(string name) => (Name) = (name);
}

var harry = new Person("Harry Potter");
var hermione = harry.With(p => p.Name, "Hermione Granger");

Console.WriteLine(hermione.Name); // Prints "Hermione Granger"

Updating multiple properties

public enum House { Gryffindor, Slytherin }

public class Person
{
    public string Name { get; }
    public House House { get; }
    
    public Person(string name, House house) => (Name, House) = (name, house);
}

var harry = new Person("Harry Potter", House.Gryffindor);
var malfoy = harry
	.With(p => p.Name, "Malfoy")
	.With(p => p.House, House.Slytherin);

Console.WriteLine(malfoy.Name); // Prints "Malfoy"
Console.WriteLine(malfoy.House); // Prints "Slytherin"

Update a property depending on its current state

public enum House { Gryffindor, Slytherin }

public class Person
{
    public string Name { get; }
    public House House { get; }
    
    public Person(string name, House house) => (Name, House) = (name, house);
}

var harry = new Person("Harry Potter", House.Gryffindor);
var malfoy = harry
	.With(p => p.House, house => house == House.Slytherin ? House.Gryffindor : house);

Console.WriteLine(malfoy.House); // Prints "Gryffindor"

How it works

The resulting type T is constructed via the following steps:

  1. Find appropriate<sup>+</sup> constructor C for type T
  2. Resolve the set of arguments A<sub>C</sub> for constructor C
  3. Invoke C, exchanging the arguments A<sub>C</sub> to be swapped
  4. Call property setters, exchanging the arguments A<sub>C</sub> to be swapped

<sup>+</sup> The constructor taking the most arguments. If there is no constructor, the default constructor is called.

In step 2, if the constructor C takes the argument to be swapped, the

Convention

Typesafe.With relies on the convention that the property is named the same in the type constructor.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.0 88 3/6/2024
1.1.1 3,832 7/12/2023
1.1.0 3,654 10/11/2022
1.0.2 10,256 1/9/2021
1.0.1 603 11/27/2020
1.0.1-alpha0002 317 11/23/2020
1.0.1-alpha0001 356 11/23/2020
1.0.0-alpha 309 11/20/2020

* Add support for interfaces
   * Update exception message when unable to find constructor parameter