W4k.Either.CodeGeneration 0.1.0-preview

This is a prerelease version of W4k.Either.CodeGeneration.
There is a newer version of this package available.
See the version list below for details.
dotnet add package W4k.Either.CodeGeneration --version 0.1.0-preview
NuGet\Install-Package W4k.Either.CodeGeneration -Version 0.1.0-preview
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="W4k.Either.CodeGeneration" Version="0.1.0-preview" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add W4k.Either.CodeGeneration --version 0.1.0-preview
#r "nuget: W4k.Either.CodeGeneration, 0.1.0-preview"
#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 W4k.Either.CodeGeneration as a Cake Addin
#addin nuget:?package=W4k.Either.CodeGeneration&version=0.1.0-preview&prerelease

// Install W4k.Either.CodeGeneration as a Cake Tool
#tool nuget:?package=W4k.Either.CodeGeneration&version=0.1.0-preview&prerelease

W4k.Either.CodeGeneration

⚠️ Code generator - despite targeting netstandard2.0 - requires project to use at least net6.0 (due to features used in generated code).

All types distributed with W4k.Either package are generated using this source generator. To see how to use those types and what is being generated, please refer to W4k.Either package.

The W4k.Either.CodeGeneration package enables you to generate your own types with custom logic. It generates types in two ways:

Generic type

Whenever you need another type with different properties representing several different things, you can declare generic type using EitherAttribute. Compiler will generate rest of type for you:

[Either]
public readonly struct Gelf<TCamille, TCat, TCrichton>
{
}

Predefined types

In case you know what types you want to use, you can specify them using EitherAttribute following way:

[Either(typeof(string), typeof(int))]
public readonly struct StringOrInt
{
}

Installation

Reference W4k.Either.CodeGeneration package in your project and use generated types:

  <ItemGroup>
    <PackageReference Include="W4k.Either.CodeGeneration" Version="0.0.0" PrivateAssets="All" />
  </ItemGroup>

Usage

Decorating your types

Declare your type as partial and decorate it with EitherAttribute. Remember to mark the containing type as partial as well if the type is nested.

Please adhere to these rules:

  • The type itself MUST be partial.
  • If the type is nested, the containing type MUST be partial as well.
  • There must be type defined either as generic type or as predefined type using attribute.

Other properties and behaviors include:

  • When declaring a generic type, it is up to you to define the type parameter constraint.
  • When implementing IEquatable<T>, the Equals and GetHashCode methods are automatically generated for you, unless you have implemented them yourself.
  • When implementing ISerializable, the serializable constructor and the GetObjectData method are automatically generated for you, unless you have implemented them yourself.
  • You are allowed to declare constructors matching those which would be generated. In that case, the constructors are not generated to avoid causing conflicts.
[Either]
[Serializable] // <- make type serializable
public readonly partial struct Polymorph<T1, T2> : IEquatable<Polymorph<T1, T2>>, ISerializable // <- `IEquatable<>` and `ISerializable` are implemented by code generator for you 
    where T1 : struct // <- generator reflects constraints in generated code
    where T2 : notnull, ICrewMember
{
    // you can declare constructor with same signature as generator would normaly produce (generator will skip it)
    public Polymorph(T2 value)
    {
        // NB! you are responsible for checking input - if you care about it ¯\_(ツ)_/¯
        ArgumentNullException.ThrowIfNull(value);
    
        // your custom logic
        if (value is Lister)
        {
            throw new ArgumentException("...");
        }
        
        // NB! you are responsible for proper initialization of the instance;
        _idx = 2;
        _v1 = default;
        _v2 = value;
    }
    
    // custom property
    public bool IsRimmer => _idx == 2 && _v2 is Rimmer;
}

Notice that value fields starts at 1, as well as state index value:

  • State is stored in field _idx:
    • 0 is reserved for invalid state (as this is default value)
    • 1, 2, ..., n are used as state index (up to 255)
  • Values are indexed from 1 as well corresponding to state index (_idx = 1 means that value is kept in field _v1)

You can declare any constructor you want, but keep in mind you need to initialize the monad properly - always set _idx and _v* fields!

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  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 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.
  • net6.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.1.0 160 7/6/2023
1.0.0 107 7/1/2023
0.2.0-preview 92 6/16/2023
0.1.0-preview 93 6/10/2023