ConstrainedComposite 0.1.0

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

// Install ConstrainedComposite as a Cake Tool
#tool nuget:?package=ConstrainedComposite&version=0.1.0                

<h1 align="center"> ConstrainedComposite </h1> <p align="center"> Constrain or Compose on the fly your .NET Types </p>

Constrain: A new tool for increased type safety

Constrain allow to emulate Refinement Types in C#.

You can ensure, that an object given to your methods has previously satisfied a condition.

Which mean, with this constraint:

struct NotZero : IConstrainer<int>
{
    public static bool TryConstrain(int arg) => arg != 0;
}

We can now make a method that take a number that cannot be 0 :

int FooBar(Constrained<int, NotZero> number) {
    return 42/number;
}

int input = GetNumber();
FooBar(input); // Type error !

var checkedInput = input.Constrain<int,NotZero>();
if(checkedInput is not null)
{
    FooBar(checkedInput.Value); // Cannot be 0 !
}

Composite: Create on the fly an anonymous structs.

Yes, anonymous types exists in C#.
But it's just an object, you can't write code that take it as input.

struct PathComposite : IComposite<string, PathComposite>
{
    readonly string[] _parts;
    private PathComposite(string core) => _parts_ = core.Split('/');

    public static bool TryCompose(in string core, [MaybeNullWhen(false)] out PathComposite composite)
    {
        // check if string is a path...
        composite = new PathComposite(core);
        return true;
    }

    public string[] Parts => _core;
}

public void ReadReport(Composition<string, PathComposite> path)
{
    // We know our path, is a path.
    var parts = path.Composite1.Parts; //And we can access the composite fields.
}
}

This could be implemented as an extension method, but this demo:

  • The fact that we can have a local state
  • That we can pass these composite object by parameter.

While we could create a new struct by hand to do the composition, sometimes it's not practical.
For example, when I'm gradualing building an object in multiples steps, in these case we would have tons of little structs for all the transitory steps, this library would allow to use anonymous composition instead of declaring a new struct.

Product Compatible and additional computed target framework versions.
.NET 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.
  • net7.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
0.1.0 207 12/8/2022