Skip to content

Instantly share code, notes, and snippets.

@geoder101
Last active February 20, 2025 20:33
Show Gist options
  • Save geoder101/0482faa16718ef0dd83eeafe0582ede5 to your computer and use it in GitHub Desktop.
Save geoder101/0482faa16718ef0dd83eeafe0582ede5 to your computer and use it in GitHub Desktop.
Experimental: C# Units of Measure
/*
The MIT License (MIT)
Copyright (c) 2025 George Dernikos <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
namespace CSharpUoM.Experimental;
/*
Inspired by:
https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/units-of-measure
https://kataskeue.com/gdp.pdf
https://gist.github.com/Savelenko/9f21c63fdc00b52a64739122176b7453
Missing ingredient:
https://github.com/dotnet/csharplang/issues/8697
*/
public interface IUnit;
public interface IQuantity<out TValue, TUnit> where TUnit : IUnit
{
TValue Value { get; }
}
public sealed record QuantifiedRef<TValue, TUnit>(TValue Value) : IQuantity<TValue, TUnit> where TUnit : IUnit
{
public static implicit operator TValue(QuantifiedRef<TValue, TUnit> q) => q.Value;
public static implicit operator QuantifiedRef<TValue, TUnit>(TValue v) => new(v);
public override string ToString() => $"{Value}<{typeof(TUnit).Name}>";
}
public readonly record struct QuantifiedVal<TValue, TUnit>(TValue Value) : IQuantity<TValue, TUnit> where TUnit : IUnit
{
public static implicit operator TValue(QuantifiedVal<TValue, TUnit> q) => q.Value;
public static implicit operator QuantifiedVal<TValue, TUnit>(TValue v) => new(v);
public override string ToString() => $"{Value}<{typeof(TUnit).Name}>";
}
/*
The MIT License (MIT)
Copyright (c) 2025 George Dernikos <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using CSharpUoM.Experimental;
using static Demo;
int v1 = 1;
int v2 = 2;
long v3 = 3;
long v4 = 4;
QuantifiedVal<int, A> q1a = v1;
QuantifiedRef<int, B> q2b = v2;
QuantifiedRef<long, A> q3a = v3;
QuantifiedVal<long, B> q4b = v4;
PrintV(v1);
PrintV(v2);
PrintV(v3);
PrintV(v4);
PrintQ(q1a);
PrintQ(q2b);
PrintQ(q3a);
PrintQ(q4b);
PrintV(q1a);
PrintV(q2b);
PrintV(q3a);
PrintV(q4b);
/*
Output:
int: 1
int: 2
long: 3
long: 4
int: 1<A>
int: 2<B>
long: 3<A>
long: 4<B>
int: 1
int: 2
long: 3
long: 4
*/
interface A : IUnit;
interface B : IUnit;
static class Demo
{
public static void PrintV(int v) => Console.WriteLine($"int: {v}");
public static void PrintV(long v) => Console.WriteLine($"long: {v}");
public static void PrintQ(IQuantity<int, A> q) => Console.WriteLine($"int: {q}");
public static void PrintQ(IQuantity<int, B> q) => Console.WriteLine($"int: {q}");
public static void PrintQ(IQuantity<long, A> q) => Console.WriteLine($"long: {q}");
public static void PrintQ(IQuantity<long, B> q) => Console.WriteLine($"long: {q}");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment