Skip to main content

Comparison by Fons Sonnemans

Nuget / site data

Nuget GitHub last commit GitHub Repo stars

Details

Info

info

Name: Comparison

A Source Generator package that generates the >, >=, <, <= operators for a partial type (class, struct or record) which implementsIComparable<T>.

Author: Fons Sonnemans

NuGet: https://www.nuget.org/packages/ReflectionIT.ComparisonOperatorsGenerator/

You can find more details at https://github.com/sonnemaf/ReflectionIT.ComparisonOperatorsGenerator

Source : https://github.com/sonnemaf/ReflectionIT.ComparisonOperatorsGenerator

Original Readme

note

ReflectionIT.ComparisonOperatorsGenerator

A Source Generator package that generates the >, >=, <, <= operators for a partial type (class, struct or record) which implements IComparable<T>.

Generating these additional operators is as simple as adding the ComparisonOperators attribute to your type. Make sure this type is partial and implements System.IComparable<T>

NuGet packages

PackageVersion
ReflectionIT.ComparisonOperatorsGeneratorNuGet

Example

Add the NuGet package and write the following code:

using ReflectionIT.ComparisonOperatorsGenerator.Attributes;

[ComparisonOperators]
partial class Point : IComparable<Point> {

public double X { get; }
public double Y { get; }

public Point(double x, double y) {
this.X = x;
this.Y = y;
}

public void Swap() => new Point(this.Y, this.X);

public double Dist => Math.Sqrt((X * X) + (Y * Y));

public override string ToString() => $"({X},{Y})";

public int CompareTo(Point? other) {
return Comparer<double?>.Default.Compare(this.Dist, other?.Dist);
}
}

This will generate the following partial class with the 4 comparison operators.

partial class Point : System.Numerics.IComparisonOperators<Point,Point,bool> 
{
public static bool operator <(Point left, Point right) => left.CompareTo(right) < 0;

public static bool operator <=(Point left, Point right) => left.CompareTo(right) <= 0;

public static bool operator >(Point left, Point right) => left.CompareTo(right) > 0;

public static bool operator >=(Point left, Point right) => left.CompareTo(right) >= 0;
}

Implement IComparisonOperators<TSelf,TOther,TResult> interface

You can automatically implement the IComparisonOperators<TSelf,TOther,TResult> interface using the ImplementIComparisonOperatorsInterface property of the ComparisonOperators attribute.

using ReflectionIT.ComparisonOperatorsGenerator.Attributes;

[ComparisonOperators(ImplementIComparisonOperatorsInterface = true)]
readonly partial record struct Time : IComparable<Time> {

public readonly int TotalMinutes;

public int Hours => TotalMinutes / 60;
public int Minutes => TotalMinutes % 60;
public Time(int totalMinutes) {
ArgumentOutOfRangeException.ThrowIfNegative(totalMinutes);
TotalMinutes = totalMinutes;
}

public Time(int hours, int minutes) : this(hours * 60 + minutes) {
}

public override string ToString() => $"{this.Hours}:{this.Minutes:00}";

public int CompareTo(Time other) => this.TotalMinutes.CompareTo(other.TotalMinutes);
}

This will generate the following partial record struct with the 4 comparison operators and the IComparisonOperators<TSelf,TOther,TResult> interface implementation

partial record struct Time : global::System.Numerics.IComparisonOperators<Time,Time,bool> 
{
public static bool operator <(Time left, Time right) => left.CompareTo(right) < 0;

public static bool operator <=(Time left, Time right) => left.CompareTo(right) <= 0;

public static bool operator >(Time left, Time right) => left.CompareTo(right) > 0;

public static bool operator >=(Time left, Time right) => left.CompareTo(right) >= 0;

}

About

note

If you want to generate comparison operators for your classes, startin with IComparable

How to use

Example ( source csproj, source files )

This is the CSharp Project that references Comparison

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>

<PackageReference Include="ReflectionIT.ComparisonOperatorsGenerator" Version="0.1.2-preview" />

</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>

</Project>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by the ReflectionIT.ComparisonOperatorsGenerator source generator
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace ComparisonDemo
{
partial class Room
{
[global::System.CodeDom.Compiler.GeneratedCode("ReflectionIT.ComparisonOperatorsGenerator", "0.1.2.0")]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
/// <summary>Compares two values to determine which is less.</summary>
/// <param name="left">The value to compare with <paramref name="right" />.</param>
/// <param name="right">The value to compare with <paramref name="left" />.</param>
/// <returns><c>true</c> if <paramref name="left" /> is less than <paramref name="right" />; otherwise, <c>false</c>.</returns>
public static bool operator <(Room left, Room right) => left.CompareTo(right) < 0;

[global::System.CodeDom.Compiler.GeneratedCode("ReflectionIT.ComparisonOperatorsGenerator", "0.1.2.0")]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
/// <summary>Compares two values to determine which is less or equal.</summary>
/// <param name="left">The value to compare with <paramref name="right" />.</param>
/// <param name="right">The value to compare with <paramref name="left" />.</param>
/// <returns><c>true</c> if <paramref name="left" /> is less than or equal to <paramref name="right" />; otherwise, <c>false</c>.</returns>
public static bool operator <=(Room left, Room right) => left.CompareTo(right) <= 0;

[global::System.CodeDom.Compiler.GeneratedCode("ReflectionIT.ComparisonOperatorsGenerator", "0.1.2.0")]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
/// <summary>Compares two values to determine which is greater.</summary>
/// <param name="left">The value to compare with <paramref name="right" />.</param>
/// <param name="right">The value to compare with <paramref name="left" />.</param>
/// <returns><c>true</c> if <paramref name="left" /> is greater than <paramref name="right" />; otherwise, <c>false</c>.</returns>
public static bool operator >(Room left, Room right) => left.CompareTo(right) > 0;

[global::System.CodeDom.Compiler.GeneratedCode("ReflectionIT.ComparisonOperatorsGenerator", "0.1.2.0")]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
/// <summary>Compares two values to determine which is greater or equal.</summary>
/// <param name="left">The value to compare with <paramref name="right" />.</param>
/// <param name="right">The value to compare with <paramref name="left" />.</param>
/// <returns><c>true</c> if <paramref name="left" /> is greater than or equal to <paramref name="right" />; otherwise, <c>false</c>.</returns>
public static bool operator >=(Room left, Room right) => left.CompareTo(right) >= 0;

}

}

Usefull

Download Example (.NET C# )

Share Comparison

https://ignatandrei.github.io/RSCG_Examples/v2/docs/Comparison

In the same category (EnhancementClass) - 27 other generators

ApparatusAOT

AspectGenerator

CommonCodeGenerator

DudNet

Enhanced.GetTypes

FastGenericNew

HsuSgSync

Immutype

Ling.Audit

Lombok.NET

M31.FluentAPI

MemberAccessor

MemoryPack

Meziantou.Polyfill

Microsoft.Extensions.Logging

Microsoft.Extensions.Options.Generators.OptionsValidatorGenerator

Microsoft.Interop.JavaScript.JSImportGenerator

OptionToStringGenerator

QueryStringGenerator

RSCG_Decorator

RSCG_UtilityTypes

StaticReflection

SyncMethodGenerator

System.Runtime.InteropServices

System.Text.RegularExpressions

TelemetryLogging

ThisClass